src/Pure/old_goals.ML
author wenzelm
Thu Oct 04 20:29:42 2007 +0200 (2007-10-04)
changeset 24850 0cfd722ab579
parent 23635 e31a814c080a
child 25685 c36e10812ae4
permissions -rw-r--r--
Name.uu, Name.aT;
wenzelm@18120
     1
(*  Title:      Pure/old_goals.ML
wenzelm@18120
     2
    ID:         $Id$
wenzelm@18120
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@18120
     4
    Copyright   1993  University of Cambridge
wenzelm@18120
     5
wenzelm@18120
     6
Old-style goal stack package.  The goal stack initially holds a dummy
wenzelm@18120
     7
proof, and can never become empty.  Each goal stack consists of a list
wenzelm@18120
     8
of levels.  The undo list is a list of goal stacks.  Finally, there
wenzelm@18120
     9
may be a stack of pending proofs.
wenzelm@18120
    10
*)
wenzelm@18120
    11
wenzelm@18120
    12
signature GOALS =
wenzelm@18120
    13
sig
wenzelm@18120
    14
  val premises: unit -> thm list
wenzelm@18120
    15
  val prove_goal: theory -> string -> (thm list -> tactic list) -> thm
wenzelm@18120
    16
  val prove_goalw: theory -> thm list -> string -> (thm list -> tactic list) -> thm
wenzelm@18120
    17
  val topthm: unit -> thm
wenzelm@18120
    18
  val result: unit -> thm
wenzelm@18120
    19
  val uresult: unit -> thm
wenzelm@18120
    20
  val getgoal: int -> term
wenzelm@18120
    21
  val gethyps: int -> thm list
wenzelm@18120
    22
  val prlev: int -> unit
wenzelm@18120
    23
  val pr: unit -> unit
wenzelm@18120
    24
  val prlim: int -> unit
wenzelm@18120
    25
  val goal: theory -> string -> thm list
wenzelm@18120
    26
  val goalw: theory -> thm list -> string -> thm list
wenzelm@18120
    27
  val Goal: string -> thm list
wenzelm@18120
    28
  val Goalw: thm list -> string -> thm list
wenzelm@18120
    29
  val by: tactic -> unit
wenzelm@18120
    30
  val back: unit -> unit
wenzelm@18120
    31
  val choplev: int -> unit
wenzelm@18120
    32
  val undo: unit -> unit
wenzelm@18120
    33
  val bind_thm: string * thm -> unit
wenzelm@18120
    34
  val bind_thms: string * thm list -> unit
wenzelm@18120
    35
  val qed: string -> unit
wenzelm@18120
    36
  val qed_goal: string -> theory -> string -> (thm list -> tactic list) -> unit
wenzelm@18120
    37
  val qed_goalw: string -> theory -> thm list -> string
wenzelm@18120
    38
    -> (thm list -> tactic list) -> unit
wenzelm@18120
    39
  val qed_spec_mp: string -> unit
wenzelm@18120
    40
  val qed_goal_spec_mp: string -> theory -> string -> (thm list -> tactic list) -> unit
wenzelm@18120
    41
  val qed_goalw_spec_mp: string -> theory -> thm list -> string
wenzelm@18120
    42
    -> (thm list -> tactic list) -> unit
wenzelm@18120
    43
  val no_qed: unit -> unit
wenzelm@22109
    44
  val inst: string -> string -> thm -> thm
wenzelm@18120
    45
end;
wenzelm@18120
    46
wenzelm@18120
    47
signature OLD_GOALS =
wenzelm@18120
    48
sig
wenzelm@18120
    49
  include GOALS
wenzelm@18120
    50
  type proof
wenzelm@19053
    51
  val chop: unit -> unit
wenzelm@18120
    52
  val reset_goals: unit -> unit
wenzelm@18120
    53
  val result_error_fn: (thm -> string -> thm) ref
wenzelm@18120
    54
  val print_sign_exn: theory -> exn -> 'a
wenzelm@18120
    55
  val prove_goalw_cterm: thm list->cterm->(thm list->tactic list)->thm
wenzelm@18120
    56
  val prove_goalw_cterm_nocheck: thm list->cterm->(thm list->tactic list)->thm
wenzelm@18120
    57
  val quick_and_dirty_prove_goalw_cterm: theory -> thm list -> cterm
wenzelm@18120
    58
    -> (thm list -> tactic list) -> thm
wenzelm@18120
    59
  val print_exn: exn -> 'a
wenzelm@18120
    60
  val filter_goal: (term*term->bool) -> thm list -> int -> thm list
wenzelm@18120
    61
  val goalw_cterm: thm list -> cterm -> thm list
wenzelm@18120
    62
  val simple_prove_goal_cterm: cterm->(thm list->tactic list)->thm
wenzelm@18120
    63
  val byev: tactic list -> unit
wenzelm@18120
    64
  val save_proof: unit -> proof
wenzelm@18120
    65
  val restore_proof: proof -> thm list
wenzelm@18120
    66
  val push_proof: unit -> unit
wenzelm@18120
    67
  val pop_proof: unit -> thm list
wenzelm@18120
    68
  val rotate_proof: unit -> thm list
wenzelm@18120
    69
  val bws: thm list -> unit
wenzelm@18120
    70
  val bw: thm -> unit
wenzelm@18120
    71
  val brs: thm list -> int -> unit
wenzelm@18120
    72
  val br: thm -> int -> unit
wenzelm@18120
    73
  val bes: thm list -> int -> unit
wenzelm@18120
    74
  val be: thm -> int -> unit
wenzelm@18120
    75
  val bds: thm list -> int -> unit
wenzelm@18120
    76
  val bd: thm -> int -> unit
wenzelm@18120
    77
  val ba: int -> unit
wenzelm@18120
    78
  val ren: string -> int -> unit
wenzelm@18120
    79
  val frs: thm list -> unit
wenzelm@18120
    80
  val fr: thm -> unit
wenzelm@18120
    81
  val fes: thm list -> unit
wenzelm@18120
    82
  val fe: thm -> unit
wenzelm@18120
    83
  val fds: thm list -> unit
wenzelm@18120
    84
  val fd: thm -> unit
wenzelm@18120
    85
  val fa: unit -> unit
wenzelm@18120
    86
end;
wenzelm@18120
    87
wenzelm@18120
    88
structure OldGoals: OLD_GOALS =
wenzelm@18120
    89
struct
wenzelm@18120
    90
wenzelm@18120
    91
(*** Goal package ***)
wenzelm@18120
    92
wenzelm@18120
    93
(*Each level of goal stack includes a proof state and alternative states,
wenzelm@18120
    94
  the output of the tactic applied to the preceeding level.  *)
wenzelm@18120
    95
type gstack = (thm * thm Seq.seq) list;
wenzelm@18120
    96
wenzelm@18120
    97
datatype proof = Proof of gstack list * thm list * (bool*thm->thm);
wenzelm@18120
    98
wenzelm@18120
    99
wenzelm@18120
   100
(*** References ***)
wenzelm@18120
   101
wenzelm@18120
   102
(*Current assumption list -- set by "goal".*)
wenzelm@18120
   103
val curr_prems = ref([] : thm list);
wenzelm@18120
   104
wenzelm@18120
   105
(*Return assumption list -- useful if you didn't save "goal"'s result. *)
wenzelm@18120
   106
fun premises() = !curr_prems;
wenzelm@18120
   107
wenzelm@18120
   108
(*Current result maker -- set by "goal", used by "result".  *)
wenzelm@18120
   109
fun init_mkresult _ = error "No goal has been supplied in subgoal module";
wenzelm@18120
   110
val curr_mkresult = ref (init_mkresult: bool*thm->thm);
wenzelm@18120
   111
wenzelm@22675
   112
val dummy = Thm.trivial (Thm.read_cterm ProtoPure.thy ("PROP No_goal_has_been_supplied", propT));
wenzelm@18120
   113
wenzelm@18120
   114
(*List of previous goal stacks, for the undo operation.  Set by setstate.
wenzelm@18120
   115
  A list of lists!*)
wenzelm@18120
   116
val undo_list = ref([[(dummy, Seq.empty)]] : gstack list);
wenzelm@18120
   117
wenzelm@18120
   118
(* Stack of proof attempts *)
wenzelm@18120
   119
val proofstack = ref([]: proof list);
wenzelm@18120
   120
wenzelm@18120
   121
(*reset all refs*)
wenzelm@18120
   122
fun reset_goals () =
wenzelm@18120
   123
  (curr_prems := []; curr_mkresult := init_mkresult;
wenzelm@18120
   124
    undo_list := [[(dummy, Seq.empty)]]);
wenzelm@18120
   125
wenzelm@18120
   126
wenzelm@18120
   127
(*** Setting up goal-directed proof ***)
wenzelm@18120
   128
wenzelm@18120
   129
(*Generates the list of new theories when the proof state's theory changes*)
wenzelm@18120
   130
fun thy_error (thy,thy') =
wenzelm@18120
   131
  let val names = Context.names_of thy' \\ Context.names_of thy
wenzelm@18120
   132
  in  case names of
wenzelm@18120
   133
        [name] => "\nNew theory: " ^ name
wenzelm@18120
   134
      | _       => "\nNew theories: " ^ space_implode ", " names
wenzelm@18120
   135
  end;
wenzelm@18120
   136
wenzelm@18120
   137
(*Default action is to print an error message; could be suppressed for
wenzelm@18120
   138
  special applications.*)
wenzelm@18120
   139
fun result_error_default state msg : thm =
wenzelm@18120
   140
  Pretty.str "Bad final proof state:" :: Display.pretty_goals (!goals_limit) state @
wenzelm@18120
   141
    [Pretty.str msg, Pretty.str "Proof failed!"] |> Pretty.chunks |> Pretty.string_of |> error;
wenzelm@18120
   142
wenzelm@18120
   143
val result_error_fn = ref result_error_default;
wenzelm@18120
   144
wenzelm@18120
   145
wenzelm@18120
   146
(*Common treatment of "goal" and "prove_goal":
wenzelm@18120
   147
  Return assumptions, initial proof state, and function to make result.
wenzelm@18120
   148
  "atomic" indicates if the goal should be wrapped up in the function
wenzelm@18120
   149
  "Goal::prop=>prop" to avoid assumptions being returned separately.
wenzelm@18120
   150
*)
wenzelm@18120
   151
fun prepare_proof atomic rths chorn =
wenzelm@18120
   152
  let
wenzelm@22826
   153
      val _ = legacy_feature "old goal command";
wenzelm@18120
   154
      val {thy, t=horn,...} = rep_cterm chorn;
wenzelm@18120
   155
      val _ = Term.no_dummy_patterns horn handle TERM (msg, _) => error msg;
wenzelm@18120
   156
      val (As, B) = Logic.strip_horn horn;
wenzelm@18120
   157
      val atoms = atomic andalso
wenzelm@23239
   158
            forall (fn t => not (can Logic.dest_implies t orelse can Logic.dest_all t)) As;
wenzelm@18120
   159
      val (As,B) = if atoms then ([],horn) else (As,B);
wenzelm@18120
   160
      val cAs = map (cterm_of thy) As;
wenzelm@18120
   161
      val prems = map (rewrite_rule rths o forall_elim_vars 0 o Thm.assume) cAs;
wenzelm@18120
   162
      val cB = cterm_of thy B;
wenzelm@18120
   163
      val st0 = let val st = Goal.init cB |> fold Thm.weaken cAs
wenzelm@18120
   164
                in  rewrite_goals_rule rths st end
wenzelm@18120
   165
      (*discharges assumptions from state in the order they appear in goal;
wenzelm@18120
   166
        checks (if requested) that resulting theorem is equivalent to goal. *)
wenzelm@18120
   167
      fun mkresult (check,state) =
wenzelm@18120
   168
        let val state = Seq.hd (flexflex_rule state)
wenzelm@18120
   169
                        handle THM _ => state   (*smash flexflex pairs*)
wenzelm@18120
   170
            val ngoals = nprems_of state
wenzelm@18120
   171
            val ath = implies_intr_list cAs state
wenzelm@18120
   172
            val th = Goal.conclude ath
wenzelm@18120
   173
            val {hyps,prop,thy=thy',...} = rep_thm th
wenzelm@18120
   174
            val final_th = standard th
wenzelm@18120
   175
        in  if not check then final_th
wenzelm@18120
   176
            else if not (eq_thy(thy,thy')) then !result_error_fn state
wenzelm@18120
   177
                ("Theory of proof state has changed!" ^
wenzelm@18120
   178
                 thy_error (thy,thy'))
wenzelm@18120
   179
            else if ngoals>0 then !result_error_fn state
wenzelm@18120
   180
                (string_of_int ngoals ^ " unsolved goals!")
wenzelm@18120
   181
            else if not (null hyps) then !result_error_fn state
wenzelm@18120
   182
                ("Additional hypotheses:\n" ^
wenzelm@18120
   183
                 cat_lines (map (Sign.string_of_term thy) hyps))
wenzelm@18120
   184
            else if Pattern.matches thy
wenzelm@18120
   185
                                    (Envir.beta_norm (term_of chorn), Envir.beta_norm prop)
wenzelm@18120
   186
                 then final_th
wenzelm@18120
   187
            else  !result_error_fn state "proved a different theorem"
wenzelm@18120
   188
        end
wenzelm@18120
   189
  in
wenzelm@18120
   190
     if eq_thy(thy, Thm.theory_of_thm st0)
wenzelm@18120
   191
     then (prems, st0, mkresult)
wenzelm@18120
   192
     else error ("Definitions would change the proof state's theory" ^
wenzelm@18120
   193
                 thy_error (thy, Thm.theory_of_thm st0))
wenzelm@18120
   194
  end
wenzelm@18120
   195
  handle THM(s,_,_) => error("prepare_proof: exception THM was raised!\n" ^ s);
wenzelm@18120
   196
wenzelm@18120
   197
(*Prints exceptions readably to users*)
wenzelm@18120
   198
fun print_sign_exn_unit thy e =
wenzelm@18120
   199
  case e of
wenzelm@18120
   200
     THM (msg,i,thms) =>
wenzelm@18120
   201
         (writeln ("Exception THM " ^ string_of_int i ^ " raised:\n" ^ msg);
wenzelm@18120
   202
          List.app print_thm thms)
wenzelm@18120
   203
   | THEORY (msg,thys) =>
wenzelm@18120
   204
         (writeln ("Exception THEORY raised:\n" ^ msg);
wenzelm@18120
   205
          List.app (writeln o Context.str_of_thy) thys)
wenzelm@18120
   206
   | TERM (msg,ts) =>
wenzelm@18120
   207
         (writeln ("Exception TERM raised:\n" ^ msg);
wenzelm@18120
   208
          List.app (writeln o Sign.string_of_term thy) ts)
wenzelm@18120
   209
   | TYPE (msg,Ts,ts) =>
wenzelm@18120
   210
         (writeln ("Exception TYPE raised:\n" ^ msg);
wenzelm@18120
   211
          List.app (writeln o Sign.string_of_typ thy) Ts;
wenzelm@18120
   212
          List.app (writeln o Sign.string_of_term thy) ts)
wenzelm@18120
   213
   | e => raise e;
wenzelm@18120
   214
wenzelm@18120
   215
(*Prints an exception, then fails*)
wenzelm@18678
   216
fun print_sign_exn thy e = (print_sign_exn_unit thy e; raise ERROR "");
wenzelm@18120
   217
wenzelm@18120
   218
(** the prove_goal.... commands
wenzelm@18120
   219
    Prove theorem using the listed tactics; check it has the specified form.
wenzelm@18120
   220
    Augment theory with all type assignments of goal.
wenzelm@18120
   221
    Syntax is similar to "goal" command for easy keyboard use. **)
wenzelm@18120
   222
wenzelm@18120
   223
(*Version taking the goal as a cterm*)
wenzelm@18120
   224
fun prove_goalw_cterm_general check rths chorn tacsf =
wenzelm@18120
   225
  let val (prems, st0, mkresult) = prepare_proof false rths chorn
wenzelm@18120
   226
      val tac = EVERY (tacsf prems)
wenzelm@18120
   227
      fun statef() =
wenzelm@18120
   228
          (case Seq.pull (tac st0) of
wenzelm@18120
   229
               SOME(st,_) => st
wenzelm@18120
   230
             | _ => error ("prove_goal: tactic failed"))
wenzelm@18120
   231
  in  mkresult (check, cond_timeit (!Output.timing) statef)  end
wenzelm@18120
   232
  handle e => (print_sign_exn_unit (#thy (rep_cterm chorn)) e;
wenzelm@18120
   233
               writeln ("The exception above was raised for\n" ^
wenzelm@18120
   234
                      Display.string_of_cterm chorn); raise e);
wenzelm@18120
   235
wenzelm@18120
   236
(*Two variants: one checking the result, one not.
wenzelm@18120
   237
  Neither prints runtime messages: they are for internal packages.*)
wenzelm@18120
   238
fun prove_goalw_cterm rths chorn =
wenzelm@18120
   239
        setmp Output.timing false (prove_goalw_cterm_general true rths chorn)
wenzelm@18120
   240
and prove_goalw_cterm_nocheck rths chorn =
wenzelm@18120
   241
        setmp Output.timing false (prove_goalw_cterm_general false rths chorn);
wenzelm@18120
   242
wenzelm@18120
   243
wenzelm@18120
   244
(*Version taking the goal as a string*)
wenzelm@18120
   245
fun prove_goalw thy rths agoal tacsf =
wenzelm@22675
   246
  let val chorn = Thm.read_cterm thy (agoal, propT)
wenzelm@18120
   247
  in prove_goalw_cterm_general true rths chorn tacsf end
wenzelm@18678
   248
  handle ERROR msg => cat_error msg (*from read_cterm?*)
wenzelm@18120
   249
                ("The error(s) above occurred for " ^ quote agoal);
wenzelm@18120
   250
wenzelm@18120
   251
(*String version with no meta-rewrite-rules*)
wenzelm@18120
   252
fun prove_goal thy = prove_goalw thy [];
wenzelm@18120
   253
wenzelm@18120
   254
(*quick and dirty version (conditional)*)
wenzelm@18120
   255
fun quick_and_dirty_prove_goalw_cterm thy rews ct tacs =
wenzelm@18120
   256
  prove_goalw_cterm rews ct
wenzelm@18120
   257
    (if ! quick_and_dirty then (K [SkipProof.cheat_tac thy]) else tacs);
wenzelm@18120
   258
wenzelm@18120
   259
wenzelm@18120
   260
(*** Commands etc ***)
wenzelm@18120
   261
wenzelm@18120
   262
(*Return the current goal stack, if any, from undo_list*)
wenzelm@18120
   263
fun getstate() : gstack = case !undo_list of
wenzelm@18120
   264
      []   => error"No current state in subgoal module"
wenzelm@18120
   265
    | x::_ => x;
wenzelm@18120
   266
wenzelm@18120
   267
(*Pops the given goal stack*)
wenzelm@18120
   268
fun pop [] = error"Cannot go back past the beginning of the proof!"
wenzelm@18120
   269
  | pop (pair::pairs) = (pair,pairs);
wenzelm@18120
   270
wenzelm@18120
   271
wenzelm@23635
   272
(* Print a level of the goal stack *)
wenzelm@18120
   273
wenzelm@18120
   274
fun print_top ((th, _), pairs) =
wenzelm@23635
   275
  let
wenzelm@23635
   276
    val n = length pairs;
wenzelm@23635
   277
    val m = (! goals_limit);
wenzelm@23635
   278
    val ngoals = nprems_of th;
wenzelm@23635
   279
  in
wenzelm@23635
   280
    [Pretty.str ("Level " ^ string_of_int n ^
wenzelm@23635
   281
      (if ngoals > 0 then " (" ^ string_of_int ngoals ^ " subgoal" ^
wenzelm@23635
   282
        (if ngoals <> 1 then "s" else "") ^ ")"
wenzelm@23635
   283
      else ""))] @
wenzelm@23635
   284
    Display.pretty_goals m th
wenzelm@23635
   285
  end |> Pretty.chunks |> Pretty.writeln;
wenzelm@18120
   286
wenzelm@18120
   287
(*Printing can raise exceptions, so the assignment occurs last.
wenzelm@18120
   288
  Can do   setstate[(st,Seq.empty)]  to set st as the state.  *)
wenzelm@18120
   289
fun setstate newgoals =
wenzelm@18120
   290
  (print_top (pop newgoals);  undo_list := newgoals :: !undo_list);
wenzelm@18120
   291
wenzelm@18120
   292
(*Given a proof state transformation, return a command that updates
wenzelm@18120
   293
    the goal stack*)
wenzelm@18120
   294
fun make_command com = setstate (com (pop (getstate())));
wenzelm@18120
   295
wenzelm@18120
   296
(*Apply a function on proof states to the current goal stack*)
wenzelm@18120
   297
fun apply_fun f = f (pop(getstate()));
wenzelm@18120
   298
wenzelm@18120
   299
(*Return the top theorem, representing the proof state*)
wenzelm@18120
   300
fun topthm () = apply_fun  (fn ((th,_), _) => th);
wenzelm@18120
   301
wenzelm@18120
   302
(*Return the final result.  *)
wenzelm@18120
   303
fun result () = !curr_mkresult (true, topthm());
wenzelm@18120
   304
wenzelm@18120
   305
(*Return the result UNCHECKED that it equals the goal -- for synthesis,
wenzelm@18120
   306
  answer extraction, or other instantiation of Vars *)
wenzelm@18120
   307
fun uresult () = !curr_mkresult (false, topthm());
wenzelm@18120
   308
wenzelm@18120
   309
(*Get subgoal i from goal stack*)
wenzelm@18120
   310
fun getgoal i = Logic.get_goal (prop_of (topthm())) i;
wenzelm@18120
   311
wenzelm@18120
   312
(*Return subgoal i's hypotheses as meta-level assumptions.
wenzelm@18120
   313
  For debugging uses of METAHYPS*)
wenzelm@18120
   314
local exception GETHYPS of thm list
wenzelm@18120
   315
in
wenzelm@18120
   316
fun gethyps i =
wenzelm@18120
   317
    (METAHYPS (fn hyps => raise (GETHYPS hyps)) i (topthm());  [])
wenzelm@18120
   318
    handle GETHYPS hyps => hyps
wenzelm@18120
   319
end;
wenzelm@18120
   320
wenzelm@18120
   321
(*Prints exceptions nicely at top level;
wenzelm@18120
   322
  raises the exception in order to have a polymorphic type!*)
wenzelm@18120
   323
fun print_exn e = (print_sign_exn_unit (Thm.theory_of_thm (topthm())) e;  raise e);
wenzelm@18120
   324
wenzelm@18120
   325
(*Which thms could apply to goal i? (debugs tactics involving filter_thms) *)
wenzelm@18120
   326
fun filter_goal could ths i = filter_thms could (999, getgoal i, ths);
wenzelm@18120
   327
wenzelm@18120
   328
(*For inspecting earlier levels of the backward proof*)
wenzelm@18120
   329
fun chop_level n (pair,pairs) =
wenzelm@18120
   330
  let val level = length pairs
wenzelm@18120
   331
  in  if n<0 andalso ~n <= level
wenzelm@18120
   332
      then  List.drop (pair::pairs, ~n)
wenzelm@18120
   333
      else if 0<=n andalso n<= level
wenzelm@18120
   334
      then  List.drop (pair::pairs, level - n)
wenzelm@18120
   335
      else  error ("Level number must lie between 0 and " ^
wenzelm@18120
   336
                   string_of_int level)
wenzelm@18120
   337
  end;
wenzelm@18120
   338
wenzelm@18120
   339
(*Print the given level of the proof; prlev ~1 prints previous level*)
wenzelm@23635
   340
fun prlev n = apply_fun (print_top o pop o (chop_level n));
wenzelm@23635
   341
fun pr () = apply_fun print_top;
wenzelm@18120
   342
wenzelm@18120
   343
(*Set goals_limit and print again*)
wenzelm@18120
   344
fun prlim n = (goals_limit:=n; pr());
wenzelm@18120
   345
wenzelm@18120
   346
(** the goal.... commands
wenzelm@18120
   347
    Read main goal.  Set global variables curr_prems, curr_mkresult.
wenzelm@18120
   348
    Initial subgoal and premises are rewritten using rths. **)
wenzelm@18120
   349
wenzelm@18120
   350
(*Version taking the goal as a cterm; if you have a term t and theory thy, use
wenzelm@18120
   351
    goalw_cterm rths (cterm_of thy t);      *)
wenzelm@18120
   352
fun agoalw_cterm atomic rths chorn =
wenzelm@18120
   353
  let val (prems, st0, mkresult) = prepare_proof atomic rths chorn
wenzelm@18120
   354
  in  undo_list := [];
wenzelm@18120
   355
      setstate [ (st0, Seq.empty) ];
wenzelm@18120
   356
      curr_prems := prems;
wenzelm@18120
   357
      curr_mkresult := mkresult;
wenzelm@18120
   358
      prems
wenzelm@18120
   359
  end;
wenzelm@18120
   360
wenzelm@18120
   361
val goalw_cterm = agoalw_cterm false;
wenzelm@18120
   362
wenzelm@18120
   363
(*Version taking the goal as a string*)
wenzelm@18120
   364
fun agoalw atomic thy rths agoal =
wenzelm@22675
   365
    agoalw_cterm atomic rths (Thm.read_cterm thy (agoal, propT))
wenzelm@18678
   366
    handle ERROR msg => cat_error msg (*from type_assign, etc via prepare_proof*)
wenzelm@18120
   367
        ("The error(s) above occurred for " ^ quote agoal);
wenzelm@18120
   368
wenzelm@18120
   369
val goalw = agoalw false;
wenzelm@18120
   370
fun goal thy = goalw thy [];
wenzelm@18120
   371
wenzelm@18120
   372
(*now the versions that wrap the goal up in `Goal' to make it atomic*)
wenzelm@22109
   373
fun Goalw thms s = agoalw true (ML_Context.the_context ()) thms s;
wenzelm@18120
   374
val Goal = Goalw [];
wenzelm@18120
   375
wenzelm@18120
   376
(*simple version with minimal amount of checking and postprocessing*)
wenzelm@18120
   377
fun simple_prove_goal_cterm G f =
wenzelm@18120
   378
  let
wenzelm@22826
   379
    val _ = legacy_feature "old goal command";
wenzelm@18120
   380
    val As = Drule.strip_imp_prems G;
wenzelm@18120
   381
    val B = Drule.strip_imp_concl G;
wenzelm@20229
   382
    val asms = map Assumption.assume As;
wenzelm@18120
   383
    fun check NONE = error "prove_goal: tactic failed"
wenzelm@18120
   384
      | check (SOME (thm, _)) = (case nprems_of thm of
wenzelm@18120
   385
            0 => thm
wenzelm@18120
   386
          | i => !result_error_fn thm (string_of_int i ^ " unsolved goals!"))
wenzelm@18120
   387
  in
wenzelm@18120
   388
    standard (implies_intr_list As
wenzelm@18120
   389
      (check (Seq.pull (EVERY (f asms) (trivial B)))))
wenzelm@18120
   390
  end;
wenzelm@18120
   391
wenzelm@18120
   392
wenzelm@18120
   393
(*Proof step "by" the given tactic -- apply tactic to the proof state*)
wenzelm@18120
   394
fun by_com tac ((th,ths), pairs) : gstack =
wenzelm@18120
   395
  (case  Seq.pull(tac th)  of
wenzelm@18120
   396
     NONE      => error"by: tactic failed"
wenzelm@18120
   397
   | SOME(th2,ths2) =>
wenzelm@22360
   398
       (if Thm.eq_thm(th,th2)
wenzelm@18120
   399
          then warning "Warning: same as previous level"
wenzelm@22360
   400
          else if Thm.eq_thm_thy(th,th2) then ()
wenzelm@18120
   401
          else warning ("Warning: theory of proof state has changed" ^
wenzelm@18120
   402
                       thy_error (Thm.theory_of_thm th, Thm.theory_of_thm th2));
wenzelm@18120
   403
       ((th2,ths2)::(th,ths)::pairs)));
wenzelm@18120
   404
wenzelm@18120
   405
fun by tac = cond_timeit (!Output.timing)
wenzelm@18120
   406
    (fn() => make_command (by_com tac));
wenzelm@18120
   407
wenzelm@18120
   408
(* byev[tac1,...,tacn] applies tac1 THEN ... THEN tacn.
wenzelm@18120
   409
   Good for debugging proofs involving prove_goal.*)
wenzelm@18120
   410
val byev = by o EVERY;
wenzelm@18120
   411
wenzelm@18120
   412
wenzelm@18120
   413
(*Backtracking means find an alternative result from a tactic.
wenzelm@18120
   414
  If none at this level, try earlier levels*)
wenzelm@18120
   415
fun backtrack [] = error"back: no alternatives"
wenzelm@18120
   416
  | backtrack ((th,thstr) :: pairs) =
wenzelm@18120
   417
     (case Seq.pull thstr of
wenzelm@18120
   418
          NONE      => (writeln"Going back a level..."; backtrack pairs)
wenzelm@18120
   419
        | SOME(th2,thstr2) =>
wenzelm@22360
   420
           (if Thm.eq_thm(th,th2)
wenzelm@18120
   421
              then warning "Warning: same as previous choice at this level"
wenzelm@22360
   422
              else if Thm.eq_thm_thy(th,th2) then ()
wenzelm@18120
   423
              else warning "Warning: theory of proof state has changed";
wenzelm@18120
   424
            (th2,thstr2)::pairs));
wenzelm@18120
   425
wenzelm@18120
   426
fun back() = setstate (backtrack (getstate()));
wenzelm@18120
   427
wenzelm@18120
   428
(*Chop back to previous level of the proof*)
wenzelm@18120
   429
fun choplev n = make_command (chop_level n);
wenzelm@18120
   430
wenzelm@18120
   431
(*Chopping back the goal stack*)
wenzelm@18120
   432
fun chop () = make_command (fn (_,pairs) => pairs);
wenzelm@18120
   433
wenzelm@18120
   434
(*Restore the previous proof state;  discard current state. *)
wenzelm@18120
   435
fun undo() = case !undo_list of
wenzelm@18120
   436
      [] => error"No proof state"
wenzelm@18120
   437
    | [_] => error"Already at initial state"
wenzelm@18120
   438
    | _::newundo =>  (undo_list := newundo;  pr()) ;
wenzelm@18120
   439
wenzelm@18120
   440
wenzelm@18120
   441
(*** Managing the proof stack ***)
wenzelm@18120
   442
wenzelm@18120
   443
fun save_proof() = Proof(!undo_list, !curr_prems, !curr_mkresult);
wenzelm@18120
   444
wenzelm@18120
   445
fun restore_proof(Proof(ul,prems,mk)) =
wenzelm@18120
   446
 (undo_list:= ul;  curr_prems:= prems;  curr_mkresult := mk;  prems);
wenzelm@18120
   447
wenzelm@18120
   448
wenzelm@18120
   449
fun top_proof() = case !proofstack of
wenzelm@18120
   450
        [] => error("Stack of proof attempts is empty!")
wenzelm@18120
   451
    | p::ps => (p,ps);
wenzelm@18120
   452
wenzelm@18120
   453
(*  push a copy of the current proof state on to the stack *)
wenzelm@18120
   454
fun push_proof() = (proofstack := (save_proof() :: !proofstack));
wenzelm@18120
   455
wenzelm@18120
   456
(* discard the top proof state of the stack *)
wenzelm@18120
   457
fun pop_proof() =
wenzelm@18120
   458
  let val (p,ps) = top_proof()
wenzelm@18120
   459
      val prems = restore_proof p
wenzelm@18120
   460
  in proofstack := ps;  pr();  prems end;
wenzelm@18120
   461
wenzelm@18120
   462
(* rotate the stack so that the top element goes to the bottom *)
wenzelm@18120
   463
fun rotate_proof() = let val (p,ps) = top_proof()
wenzelm@18120
   464
                    in proofstack := ps@[save_proof()];
wenzelm@18120
   465
                       restore_proof p;
wenzelm@18120
   466
                       pr();
wenzelm@18120
   467
                       !curr_prems
wenzelm@18120
   468
                    end;
wenzelm@18120
   469
wenzelm@18120
   470
wenzelm@18120
   471
(** Shortcuts for commonly-used tactics **)
wenzelm@18120
   472
wenzelm@18120
   473
fun bws rls = by (rewrite_goals_tac rls);
wenzelm@18120
   474
fun bw rl = bws [rl];
wenzelm@18120
   475
wenzelm@18120
   476
fun brs rls i = by (resolve_tac rls i);
wenzelm@18120
   477
fun br rl = brs [rl];
wenzelm@18120
   478
wenzelm@18120
   479
fun bes rls i = by (eresolve_tac rls i);
wenzelm@18120
   480
fun be rl = bes [rl];
wenzelm@18120
   481
wenzelm@18120
   482
fun bds rls i = by (dresolve_tac rls i);
wenzelm@18120
   483
fun bd rl = bds [rl];
wenzelm@18120
   484
wenzelm@18120
   485
fun ba i = by (assume_tac i);
wenzelm@18120
   486
wenzelm@18120
   487
fun ren str i = by (rename_tac str i);
wenzelm@18120
   488
wenzelm@18120
   489
(** Shortcuts to work on the first applicable subgoal **)
wenzelm@18120
   490
wenzelm@18120
   491
fun frs rls = by (FIRSTGOAL (trace_goalno_tac (resolve_tac rls)));
wenzelm@18120
   492
fun fr rl = frs [rl];
wenzelm@18120
   493
wenzelm@18120
   494
fun fes rls = by (FIRSTGOAL (trace_goalno_tac (eresolve_tac rls)));
wenzelm@18120
   495
fun fe rl = fes [rl];
wenzelm@18120
   496
wenzelm@18120
   497
fun fds rls = by (FIRSTGOAL (trace_goalno_tac (dresolve_tac rls)));
wenzelm@18120
   498
fun fd rl = fds [rl];
wenzelm@18120
   499
wenzelm@18120
   500
fun fa() = by (FIRSTGOAL (trace_goalno_tac assume_tac));
wenzelm@18120
   501
wenzelm@18120
   502
wenzelm@19053
   503
(** theorem database **)
wenzelm@18120
   504
wenzelm@18120
   505
fun bind_thm (name, thm) = ThmDatabase.ml_store_thm (name, standard thm);
wenzelm@18120
   506
fun bind_thms (name, thms) = ThmDatabase.ml_store_thms (name, map standard thms);
wenzelm@18120
   507
wenzelm@18120
   508
fun qed name = ThmDatabase.ml_store_thm (name, result ());
wenzelm@18120
   509
fun qed_goal name thy goal tacsf = ThmDatabase.ml_store_thm (name, prove_goal thy goal tacsf);
wenzelm@18120
   510
fun qed_goalw name thy rews goal tacsf =
wenzelm@18120
   511
  ThmDatabase.ml_store_thm (name, prove_goalw thy rews goal tacsf);
wenzelm@18120
   512
fun qed_spec_mp name =
wenzelm@18120
   513
  ThmDatabase.ml_store_thm (name, ObjectLogic.rulify_no_asm (result ()));
wenzelm@18120
   514
fun qed_goal_spec_mp name thy s p =
wenzelm@18120
   515
  bind_thm (name, ObjectLogic.rulify_no_asm (prove_goal thy s p));
wenzelm@18120
   516
fun qed_goalw_spec_mp name thy defs s p =
wenzelm@18120
   517
  bind_thm (name, ObjectLogic.rulify_no_asm (prove_goalw thy defs s p));
wenzelm@18120
   518
wenzelm@18120
   519
fun no_qed () = ();
wenzelm@18120
   520
wenzelm@22109
   521
(*shorthand for instantiating just one variable in the current theory*)
wenzelm@22109
   522
fun inst x t = read_instantiate_sg (the_context()) [(x,t)];
wenzelm@22109
   523
wenzelm@18120
   524
end;
wenzelm@18120
   525
wenzelm@18120
   526
structure Goals: GOALS = OldGoals;
wenzelm@18120
   527
open Goals;