src/Pure/goals.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 3669 3384c6f1f095
child 3707 40856b593501
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
clasohm@1460
     1
(*  Title: 	Pure/goals.ML
clasohm@0
     2
    ID:         $Id$
clasohm@1460
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Goal stack package.  The goal stack initially holds a dummy proof, and can
clasohm@0
     7
never become empty.  Each goal stack consists of a list of levels.  The
clasohm@0
     8
undo list is a list of goal stacks.  Finally, there may be a stack of
clasohm@0
     9
pending proofs.
clasohm@0
    10
*)
clasohm@0
    11
clasohm@0
    12
clasohm@0
    13
signature GOALS =
clasohm@0
    14
  sig
clasohm@0
    15
  type proof
clasohm@1460
    16
  val ba		: int -> unit
clasohm@1460
    17
  val back		: unit -> unit
clasohm@1460
    18
  val bd		: thm -> int -> unit
clasohm@1460
    19
  val bds		: thm list -> int -> unit
clasohm@1460
    20
  val be		: thm -> int -> unit
clasohm@1460
    21
  val bes		: thm list -> int -> unit
clasohm@1460
    22
  val br		: thm -> int -> unit
clasohm@1460
    23
  val brs		: thm list -> int -> unit
clasohm@1460
    24
  val bw		: thm -> unit
clasohm@1460
    25
  val bws		: thm list -> unit
clasohm@1460
    26
  val by		: tactic -> unit
clasohm@1460
    27
  val byev		: tactic list -> unit
clasohm@1460
    28
  val chop		: unit -> unit
clasohm@1460
    29
  val choplev		: int -> unit
clasohm@1460
    30
  val fa		: unit -> unit
clasohm@1460
    31
  val fd		: thm -> unit
clasohm@1460
    32
  val fds		: thm list -> unit
clasohm@1460
    33
  val fe		: thm -> unit
clasohm@1460
    34
  val fes		: thm list -> unit
clasohm@1460
    35
  val filter_goal	: (term*term->bool) -> thm list -> int -> thm list
clasohm@1460
    36
  val fr		: thm -> unit
clasohm@1460
    37
  val frs		: thm list -> unit
clasohm@1460
    38
  val getgoal		: int -> term
clasohm@1460
    39
  val gethyps		: int -> thm list
clasohm@1460
    40
  val goal		: theory -> string -> thm list
clasohm@1460
    41
  val goalw		: theory -> thm list -> string -> thm list
clasohm@1460
    42
  val goalw_cterm	: thm list -> cterm -> thm list
wenzelm@3532
    43
  val print_current_goals_fn : (int -> int -> thm -> unit) ref
clasohm@1460
    44
  val pop_proof		: unit -> thm list
clasohm@1460
    45
  val pr		: unit -> unit
paulson@2580
    46
  val prlev		: int -> unit
paulson@2514
    47
  val prlim		: int -> unit
berghofe@1628
    48
  val pr_latex		: unit -> unit
berghofe@1628
    49
  val printgoal_latex	: int -> unit
clasohm@1460
    50
  val premises		: unit -> thm list
clasohm@1460
    51
  val prin		: term -> unit
clasohm@1460
    52
  val printyp		: typ -> unit
clasohm@1460
    53
  val pprint_term	: term -> pprint_args -> unit
clasohm@1460
    54
  val pprint_typ	: typ -> pprint_args -> unit
clasohm@1460
    55
  val print_exn		: exn -> 'a
clasohm@1460
    56
  val print_sign_exn	: Sign.sg -> exn -> 'a
clasohm@1460
    57
  val proof_timing	: bool ref
clasohm@1460
    58
  val prove_goal	: theory -> string -> (thm list -> tactic list) -> thm
lcp@577
    59
  val prove_goalw      : theory->thm list->string->(thm list->tactic list)->thm
clasohm@1460
    60
  val prove_goalw_cterm	: thm list->cterm->(thm list->tactic list)->thm
clasohm@1460
    61
  val push_proof	: unit -> unit
clasohm@1460
    62
  val read		: string -> term
clasohm@1460
    63
  val ren		: string -> int -> unit
clasohm@1460
    64
  val restore_proof	: proof -> thm list
clasohm@1460
    65
  val result		: unit -> thm  
wenzelm@3532
    66
  val result_error_fn   : (thm -> string -> thm) ref
clasohm@1460
    67
  val rotate_proof	: unit -> thm list
clasohm@1460
    68
  val uresult		: unit -> thm  
clasohm@1460
    69
  val save_proof	: unit -> proof
clasohm@1460
    70
  val topthm		: unit -> thm
clasohm@1460
    71
  val undo		: unit -> unit
clasohm@0
    72
  end;
clasohm@0
    73
paulson@1500
    74
structure Goals : GOALS =
clasohm@0
    75
struct
clasohm@0
    76
clasohm@0
    77
(*Each level of goal stack includes a proof state and alternative states,
clasohm@0
    78
  the output of the tactic applied to the preceeding level.  *)
clasohm@0
    79
type gstack = (thm * thm Sequence.seq) list;
clasohm@0
    80
clasohm@0
    81
datatype proof = Proof of gstack list * thm list * (bool*thm->thm);
clasohm@0
    82
clasohm@0
    83
(*** References ***)
clasohm@0
    84
clasohm@0
    85
(*Should process time be printed after proof steps?*)
clasohm@0
    86
val proof_timing = ref false;
clasohm@0
    87
clasohm@0
    88
(*Current assumption list -- set by "goal".*)
clasohm@0
    89
val curr_prems = ref([] : thm list);
clasohm@0
    90
clasohm@0
    91
(*Return assumption list -- useful if you didn't save "goal"'s result. *)
clasohm@0
    92
fun premises() = !curr_prems;
clasohm@0
    93
clasohm@0
    94
(*Current result maker -- set by "goal", used by "result".  *)
clasohm@0
    95
val curr_mkresult = 
clasohm@0
    96
    ref((fn _=> error"No goal has been supplied in subgoal module") 
clasohm@0
    97
       : bool*thm->thm);
clasohm@0
    98
wenzelm@2878
    99
val dummy = trivial(read_cterm Sign.pure 
clasohm@0
   100
    ("PROP No_goal_has_been_supplied",propT));
clasohm@0
   101
clasohm@0
   102
(*List of previous goal stacks, for the undo operation.  Set by setstate. 
clasohm@0
   103
  A list of lists!*)
clasohm@0
   104
val undo_list = ref([[(dummy, Sequence.null)]] : gstack list);
clasohm@0
   105
clasohm@0
   106
(* Stack of proof attempts *)
clasohm@0
   107
val proofstack = ref([]: proof list);
clasohm@0
   108
clasohm@0
   109
clasohm@0
   110
(*** Setting up goal-directed proof ***)
clasohm@0
   111
clasohm@0
   112
(*Generates the list of new theories when the proof state's signature changes*)
clasohm@0
   113
fun sign_error (sign,sign') =
clasohm@0
   114
  let val stamps = #stamps(Sign.rep_sg sign') \\ 
clasohm@0
   115
                   #stamps(Sign.rep_sg sign)
clasohm@0
   116
  in  case stamps of
clasohm@0
   117
        [stamp] => "\nNew theory: " ^ !stamp
clasohm@0
   118
      | _       => "\nNew theories: " ^ space_implode ", " (map ! stamps)
clasohm@0
   119
  end;
clasohm@0
   120
paulson@1928
   121
(*Default action is to print an error message; could be suppressed for
paulson@1928
   122
  special applications.*)
wenzelm@3669
   123
fun result_error_default state msg : thm =
wenzelm@3669
   124
 (writeln "Bad final proof state:";
wenzelm@3669
   125
  print_goals (!goals_limit) state;
wenzelm@3669
   126
  writeln msg; raise ERROR);
paulson@1928
   127
wenzelm@3532
   128
val result_error_fn = ref result_error_default;
paulson@1928
   129
clasohm@0
   130
(*Common treatment of "goal" and "prove_goal":
clasohm@0
   131
  Return assumptions, initial proof state, and function to make result. *)
clasohm@0
   132
fun prepare_proof rths chorn =
lcp@230
   133
  let val {sign, t=horn,...} = rep_cterm chorn;
clasohm@0
   134
      val (_,As,B) = Logic.strip_horn(horn);
lcp@230
   135
      val cAs = map (cterm_of sign) As;
paulson@1413
   136
      val prems = map (rewrite_rule rths o forall_elim_vars(0) o assume) cAs
paulson@1413
   137
      and st0 = (rewrite_goals_rule rths o trivial) (cterm_of sign B)
clasohm@0
   138
      (*discharges assumptions from state in the order they appear in goal;
clasohm@1460
   139
	checks (if requested) that resulting theorem is equivalent to goal. *)
clasohm@0
   140
      fun mkresult (check,state) =
lcp@696
   141
        let val state = Sequence.hd (flexflex_rule state)
paulson@1565
   142
	    		handle THM _ => state   (*smash flexflex pairs*)
clasohm@1460
   143
	    val ngoals = nprems_of state
wenzelm@1219
   144
            val th = strip_shyps (implies_intr_list cAs state)
wenzelm@1240
   145
            val {hyps,prop,sign=sign',...} = rep_thm th
wenzelm@1240
   146
            val xshyps = extra_shyps th;
paulson@2169
   147
        in  if not check then th
wenzelm@3532
   148
	    else if not (Sign.eq_sg(sign,sign')) then !result_error_fn state
clasohm@1460
   149
		("Signature of proof state has changed!" ^
clasohm@1460
   150
		 sign_error (sign,sign'))
wenzelm@3532
   151
            else if ngoals>0 then !result_error_fn state
clasohm@1460
   152
		(string_of_int ngoals ^ " unsolved goals!")
wenzelm@3532
   153
            else if not (null hyps) then !result_error_fn state
clasohm@0
   154
                ("Additional hypotheses:\n" ^ 
clasohm@0
   155
                 cat_lines (map (Sign.string_of_term sign) hyps))
wenzelm@3532
   156
	    else if not (null xshyps) then !result_error_fn state
wenzelm@1240
   157
                ("Extra sort hypotheses: " ^
wenzelm@2962
   158
                 commas (map Sorts.str_of_sort xshyps))
clasohm@1460
   159
	    else if Pattern.matches (#tsig(Sign.rep_sg sign)) 
clasohm@1460
   160
			            (term_of chorn, prop)
clasohm@1460
   161
		 then  standard th 
wenzelm@3532
   162
	    else  !result_error_fn state "proved a different theorem"
clasohm@0
   163
        end
nipkow@678
   164
  in
nipkow@678
   165
     if Sign.eq_sg(sign, #sign(rep_thm st0))
clasohm@0
   166
     then (prems, st0, mkresult)
clasohm@0
   167
     else error ("Definitions would change the proof state's signature" ^
clasohm@1460
   168
		 sign_error (sign, #sign(rep_thm st0)))
clasohm@0
   169
  end
clasohm@0
   170
  handle THM(s,_,_) => error("prepare_proof: exception THM was raised!\n" ^ s);
clasohm@0
   171
clasohm@0
   172
(*Prints exceptions readably to users*)
lcp@577
   173
fun print_sign_exn_unit sign e = 
clasohm@0
   174
  case e of
clasohm@0
   175
     THM (msg,i,thms) =>
clasohm@1460
   176
	 (writeln ("Exception THM " ^ string_of_int i ^ " raised:\n" ^ msg);
clasohm@1460
   177
	  seq print_thm thms)
clasohm@0
   178
   | THEORY (msg,thys) =>
clasohm@1460
   179
	 (writeln ("Exception THEORY raised:\n" ^ msg);
clasohm@1460
   180
	  seq print_theory thys)
clasohm@0
   181
   | TERM (msg,ts) =>
clasohm@1460
   182
	 (writeln ("Exception TERM raised:\n" ^ msg);
clasohm@1460
   183
	  seq (writeln o Sign.string_of_term sign) ts)
clasohm@0
   184
   | TYPE (msg,Ts,ts) =>
clasohm@1460
   185
	 (writeln ("Exception TYPE raised:\n" ^ msg);
clasohm@1460
   186
	  seq (writeln o Sign.string_of_typ sign) Ts;
clasohm@1460
   187
	  seq (writeln o Sign.string_of_term sign) ts)
clasohm@0
   188
   | e => raise e;
clasohm@0
   189
lcp@577
   190
(*Prints an exception, then fails*)
lcp@577
   191
fun print_sign_exn sign e = (print_sign_exn_unit sign e; raise ERROR);
lcp@577
   192
clasohm@0
   193
(** the prove_goal.... commands
clasohm@0
   194
    Prove theorem using the listed tactics; check it has the specified form.
clasohm@0
   195
    Augment signature with all type assignments of goal.
clasohm@0
   196
    Syntax is similar to "goal" command for easy keyboard use. **)
clasohm@0
   197
clasohm@0
   198
(*Version taking the goal as a cterm*)
clasohm@0
   199
fun prove_goalw_cterm rths chorn tacsf =
clasohm@0
   200
  let val (prems, st0, mkresult) = prepare_proof rths chorn
clasohm@0
   201
      val tac = EVERY (tacsf prems)
clasohm@0
   202
      fun statef() = 
paulson@1500
   203
	  (case Sequence.pull (tac st0) of 
clasohm@1460
   204
	       Some(st,_) => st
clasohm@1460
   205
	     | _ => error ("prove_goal: tactic failed"))
lcp@545
   206
  in  mkresult (true, cond_timeit (!proof_timing) statef)  end
lcp@914
   207
  handle e => (print_sign_exn_unit (#sign (rep_cterm chorn)) e;
clasohm@1460
   208
	       error ("The exception above was raised for\n" ^ 
clasohm@1460
   209
		      string_of_cterm chorn));
lcp@545
   210
clasohm@0
   211
clasohm@0
   212
(*Version taking the goal as a string*)
clasohm@0
   213
fun prove_goalw thy rths agoal tacsf =
clasohm@0
   214
  let val sign = sign_of thy
lcp@230
   215
      val chorn = read_cterm sign (agoal,propT)
lcp@545
   216
  in  prove_goalw_cterm rths chorn tacsf end
lcp@545
   217
  handle ERROR => error (*from read_cterm?*)
wenzelm@3536
   218
		("The error(s) above occurred for " ^ quote agoal);
clasohm@0
   219
clasohm@0
   220
(*String version with no meta-rewrite-rules*)
clasohm@0
   221
fun prove_goal thy = prove_goalw thy [];
clasohm@0
   222
clasohm@0
   223
clasohm@0
   224
(*** Commands etc ***)
clasohm@0
   225
clasohm@0
   226
(*Return the current goal stack, if any, from undo_list*)
clasohm@0
   227
fun getstate() : gstack = case !undo_list of 
clasohm@0
   228
      []   => error"No current state in subgoal module"
clasohm@0
   229
    | x::_ => x;
clasohm@0
   230
clasohm@0
   231
(*Pops the given goal stack*)
clasohm@0
   232
fun pop [] = error"Cannot go back past the beginning of the proof!"
clasohm@0
   233
  | pop (pair::pairs) = (pair,pairs);
clasohm@0
   234
clasohm@0
   235
wenzelm@3532
   236
(*Print goals of current level*)
wenzelm@3532
   237
fun print_current_goals_default n max th =
wenzelm@3669
   238
  (writeln ("Level " ^ string_of_int n); print_goals max th);
wenzelm@3532
   239
wenzelm@3532
   240
val print_current_goals_fn = ref print_current_goals_default;
wenzelm@3532
   241
clasohm@0
   242
(*Print a level of the goal stack.*)
wenzelm@3532
   243
fun print_top ((th, _), pairs) =
wenzelm@3532
   244
  !print_current_goals_fn (length pairs) (!goals_limit) th;
clasohm@0
   245
clasohm@0
   246
(*Printing can raise exceptions, so the assignment occurs last.
clasohm@0
   247
  Can do   setstate[(st,Sequence.null)]  to set st as the state.  *)
clasohm@0
   248
fun setstate newgoals = 
clasohm@0
   249
  (print_top (pop newgoals);  undo_list := newgoals :: !undo_list);
clasohm@0
   250
clasohm@0
   251
(*Given a proof state transformation, return a command that updates
clasohm@0
   252
    the goal stack*)
clasohm@0
   253
fun make_command com = setstate (com (pop (getstate())));
clasohm@0
   254
clasohm@0
   255
(*Apply a function on proof states to the current goal stack*)
clasohm@0
   256
fun apply_fun f = f (pop(getstate()));
clasohm@0
   257
clasohm@0
   258
(*Return the top theorem, representing the proof state*)
clasohm@0
   259
fun topthm () = apply_fun  (fn ((th,_), _) => th);
clasohm@0
   260
clasohm@0
   261
(*Return the final result.  *)
clasohm@0
   262
fun result () = !curr_mkresult (true, topthm());
clasohm@0
   263
clasohm@0
   264
(*Return the result UNCHECKED that it equals the goal -- for synthesis,
clasohm@0
   265
  answer extraction, or other instantiation of Vars *)
clasohm@0
   266
fun uresult () = !curr_mkresult (false, topthm());
clasohm@0
   267
clasohm@0
   268
(*Get subgoal i from goal stack*)
paulson@2580
   269
fun getgoal i = List.nth (prems_of (topthm()), i-1)
paulson@2580
   270
                handle Subscript => error"getgoal: Goal number out of range";
clasohm@0
   271
clasohm@0
   272
(*Return subgoal i's hypotheses as meta-level assumptions.
clasohm@0
   273
  For debugging uses of METAHYPS*)
clasohm@0
   274
local exception GETHYPS of thm list
clasohm@0
   275
in
clasohm@0
   276
fun gethyps i = 
paulson@1500
   277
    (METAHYPS (fn hyps => raise (GETHYPS hyps)) i (topthm());  [])
clasohm@0
   278
    handle GETHYPS hyps => hyps
clasohm@0
   279
end;
clasohm@0
   280
clasohm@0
   281
(*Which thms could apply to goal i? (debugs tactics involving filter_thms) *)
clasohm@0
   282
fun filter_goal could ths i = filter_thms could (999, getgoal i, ths);
clasohm@0
   283
clasohm@0
   284
(*For inspecting earlier levels of the backward proof*)
clasohm@0
   285
fun chop_level n (pair,pairs) = 
clasohm@0
   286
  let val level = length pairs
paulson@2126
   287
  in  if n<0 andalso ~n <= level
paulson@2580
   288
      then  List.drop (pair::pairs, ~n) 
paulson@2126
   289
      else if 0<=n andalso n<= level
paulson@2580
   290
      then  List.drop (pair::pairs, level - n) 
clasohm@0
   291
      else  error ("Level number must lie between 0 and " ^ 
clasohm@1460
   292
		   string_of_int level)
clasohm@0
   293
  end;
clasohm@0
   294
paulson@2514
   295
(*Print the given level of the proof; prlev ~1 prints previous level*)
clasohm@0
   296
fun prlev n = apply_fun (print_top o pop o (chop_level n));
clasohm@0
   297
fun pr () = apply_fun print_top;
clasohm@0
   298
paulson@2514
   299
(*Set goals_limit and print again*)
paulson@2514
   300
fun prlim n = (goals_limit:=n; pr());
paulson@2514
   301
clasohm@0
   302
(** the goal.... commands
clasohm@0
   303
    Read main goal.  Set global variables curr_prems, curr_mkresult. 
clasohm@0
   304
    Initial subgoal and premises are rewritten using rths. **)
clasohm@0
   305
clasohm@0
   306
(*Version taking the goal as a cterm; if you have a term t and theory thy, use
lcp@230
   307
    goalw_cterm rths (cterm_of (sign_of thy) t);      *)
clasohm@0
   308
fun goalw_cterm rths chorn = 
clasohm@0
   309
  let val (prems, st0, mkresult) = prepare_proof rths chorn
clasohm@0
   310
  in  undo_list := [];
clasohm@0
   311
      setstate [ (st0, Sequence.null) ];  
clasohm@0
   312
      curr_prems := prems;
clasohm@0
   313
      curr_mkresult := mkresult;
clasohm@0
   314
      prems
clasohm@0
   315
  end;
clasohm@0
   316
clasohm@0
   317
(*Version taking the goal as a string*)
clasohm@0
   318
fun goalw thy rths agoal = 
lcp@230
   319
  goalw_cterm rths (read_cterm(sign_of thy)(agoal,propT))
clasohm@0
   320
  handle ERROR => error (*from type_assign, etc via prepare_proof*)
wenzelm@3536
   321
	    ("The error(s) above occurred for " ^ quote agoal);
clasohm@0
   322
clasohm@0
   323
(*String version with no meta-rewrite-rules*)
clasohm@0
   324
fun goal thy = goalw thy [];
clasohm@0
   325
clasohm@0
   326
(*Proof step "by" the given tactic -- apply tactic to the proof state*)
clasohm@0
   327
fun by_com tac ((th,ths), pairs) : gstack =
paulson@1500
   328
  (case  Sequence.pull(tac th)  of
clasohm@0
   329
     None      => error"by: tactic failed"
clasohm@0
   330
   | Some(th2,ths2) => 
clasohm@0
   331
       (if eq_thm(th,th2) 
wenzelm@3532
   332
	  then writeln "Warning: same as previous level"
clasohm@1460
   333
	  else if eq_thm_sg(th,th2) then ()
wenzelm@3532
   334
	  else writeln ("Warning: signature of proof state has changed" ^
clasohm@1460
   335
		       sign_error (#sign(rep_thm th), #sign(rep_thm th2)));
clasohm@0
   336
       ((th2,ths2)::(th,ths)::pairs)));
clasohm@0
   337
clasohm@0
   338
fun by tac = cond_timeit (!proof_timing) 
clasohm@0
   339
    (fn() => make_command (by_com tac));
clasohm@0
   340
clasohm@0
   341
(* byev[tac1,...,tacn] applies tac1 THEN ... THEN tacn.
clasohm@0
   342
   Good for debugging proofs involving prove_goal.*)
clasohm@0
   343
val byev = by o EVERY;
clasohm@0
   344
clasohm@0
   345
clasohm@0
   346
(*Backtracking means find an alternative result from a tactic.
clasohm@0
   347
  If none at this level, try earlier levels*)
clasohm@0
   348
fun backtrack [] = error"back: no alternatives"
clasohm@0
   349
  | backtrack ((th,thstr) :: pairs) =
clasohm@0
   350
     (case Sequence.pull thstr of
clasohm@1460
   351
	  None      => (writeln"Going back a level..."; backtrack pairs)
clasohm@1460
   352
	| Some(th2,thstr2) =>  
clasohm@1460
   353
	   (if eq_thm(th,th2) 
wenzelm@3532
   354
	      then writeln "Warning: same as previous choice at this level"
clasohm@1460
   355
	      else if eq_thm_sg(th,th2) then ()
wenzelm@3532
   356
	      else writeln "Warning: signature of proof state has changed";
clasohm@1460
   357
	    (th2,thstr2)::pairs));
clasohm@0
   358
clasohm@0
   359
fun back() = setstate (backtrack (getstate()));
clasohm@0
   360
clasohm@0
   361
(*Chop back to previous level of the proof*)
clasohm@0
   362
fun choplev n = make_command (chop_level n);
clasohm@0
   363
clasohm@0
   364
(*Chopping back the goal stack*)
clasohm@0
   365
fun chop () = make_command (fn (_,pairs) => pairs);
clasohm@0
   366
clasohm@0
   367
(*Restore the previous proof state;  discard current state. *)
clasohm@0
   368
fun undo() = case !undo_list of
clasohm@0
   369
      [] => error"No proof state"
clasohm@0
   370
    | [_] => error"Already at initial state"
clasohm@0
   371
    | _::newundo =>  (undo_list := newundo;  pr()) ;
clasohm@0
   372
clasohm@0
   373
clasohm@0
   374
(*** Managing the proof stack ***)
clasohm@0
   375
clasohm@0
   376
fun save_proof() = Proof(!undo_list, !curr_prems, !curr_mkresult);
clasohm@0
   377
clasohm@0
   378
fun restore_proof(Proof(ul,prems,mk)) =
clasohm@0
   379
 (undo_list:= ul;  curr_prems:= prems;  curr_mkresult := mk;  prems);
clasohm@0
   380
clasohm@0
   381
clasohm@0
   382
fun top_proof() = case !proofstack of
clasohm@1460
   383
	[] => error("Stack of proof attempts is empty!")
clasohm@0
   384
    | p::ps => (p,ps);
clasohm@0
   385
clasohm@0
   386
(*  push a copy of the current proof state on to the stack *)
clasohm@0
   387
fun push_proof() = (proofstack := (save_proof() :: !proofstack));
clasohm@0
   388
clasohm@0
   389
(* discard the top proof state of the stack *)
clasohm@0
   390
fun pop_proof() = 
clasohm@0
   391
  let val (p,ps) = top_proof()
clasohm@0
   392
      val prems = restore_proof p
clasohm@0
   393
  in proofstack := ps;  pr();  prems end;
clasohm@0
   394
clasohm@0
   395
(* rotate the stack so that the top element goes to the bottom *)
clasohm@0
   396
fun rotate_proof() = let val (p,ps) = top_proof()
clasohm@1460
   397
		    in proofstack := ps@[save_proof()];
clasohm@1460
   398
		       restore_proof p;
clasohm@1460
   399
		       pr();
clasohm@1460
   400
		       !curr_prems
clasohm@1460
   401
		    end;
clasohm@0
   402
clasohm@0
   403
clasohm@0
   404
(** Shortcuts for commonly-used tactics **)
clasohm@0
   405
clasohm@0
   406
fun bws rls = by (rewrite_goals_tac rls);
clasohm@0
   407
fun bw rl = bws [rl];
clasohm@0
   408
clasohm@0
   409
fun brs rls i = by (resolve_tac rls i);
clasohm@0
   410
fun br rl = brs [rl];
clasohm@0
   411
clasohm@0
   412
fun bes rls i = by (eresolve_tac rls i);
clasohm@0
   413
fun be rl = bes [rl];
clasohm@0
   414
clasohm@0
   415
fun bds rls i = by (dresolve_tac rls i);
clasohm@0
   416
fun bd rl = bds [rl];
clasohm@0
   417
clasohm@0
   418
fun ba i = by (assume_tac i);
clasohm@0
   419
clasohm@0
   420
fun ren str i = by (rename_tac str i);
clasohm@0
   421
clasohm@0
   422
(** Shortcuts to work on the first applicable subgoal **)
clasohm@0
   423
clasohm@0
   424
fun frs rls = by (FIRSTGOAL (trace_goalno_tac (resolve_tac rls)));
clasohm@0
   425
fun fr rl = frs [rl];
clasohm@0
   426
clasohm@0
   427
fun fes rls = by (FIRSTGOAL (trace_goalno_tac (eresolve_tac rls)));
clasohm@0
   428
fun fe rl = fes [rl];
clasohm@0
   429
clasohm@0
   430
fun fds rls = by (FIRSTGOAL (trace_goalno_tac (dresolve_tac rls)));
clasohm@0
   431
fun fd rl = fds [rl];
clasohm@0
   432
clasohm@0
   433
fun fa() = by (FIRSTGOAL (trace_goalno_tac assume_tac));
clasohm@0
   434
clasohm@0
   435
(** Reading and printing terms wrt the current theory **)
clasohm@0
   436
clasohm@0
   437
fun top_sg() = #sign(rep_thm(topthm()));
clasohm@0
   438
lcp@230
   439
fun read s = term_of (read_cterm (top_sg())
clasohm@1460
   440
			                   (s, (TVar(("DUMMY",0),[]))));
clasohm@0
   441
clasohm@0
   442
(*Print a term under the current signature of the proof state*)
clasohm@0
   443
fun prin t = writeln (Sign.string_of_term (top_sg()) t);
clasohm@0
   444
clasohm@0
   445
fun printyp T = writeln (Sign.string_of_typ (top_sg()) T);
clasohm@0
   446
clasohm@0
   447
fun pprint_term t = Sign.pprint_term (top_sg()) t;
clasohm@0
   448
clasohm@0
   449
fun pprint_typ T = Sign.pprint_typ (top_sg()) T;
clasohm@0
   450
berghofe@1628
   451
(* Redirect output of function f:unit->unit to LaTeX *)
berghofe@1628
   452
fun redirect_to_latex f =
berghofe@1628
   453
	let
berghofe@1628
   454
		val s = ref ""
berghofe@1628
   455
		val old_prs_fn = !prs_fn
berghofe@1628
   456
	in
berghofe@1628
   457
		(prs_fn := (fn a => s := !s ^ a);
berghofe@1628
   458
		f ();
berghofe@1628
   459
		latex (!s);
berghofe@1628
   460
		prs_fn := old_prs_fn)
berghofe@1628
   461
	end;
berghofe@1628
   462
berghofe@1628
   463
(* Display current proof state in xdvi window *)
berghofe@1628
   464
fun pr_latex () = redirect_to_latex pr;
berghofe@1628
   465
berghofe@1628
   466
(* Display goal n of current proof state in xdvi window *)
berghofe@1628
   467
fun printgoal_latex n = redirect_to_latex (fn () => prin(getgoal n));
berghofe@1628
   468
clasohm@0
   469
(*Prints exceptions nicely at top level; 
clasohm@0
   470
  raises the exception in order to have a polymorphic type!*)
lcp@914
   471
fun print_exn e = (print_sign_exn_unit (top_sg()) e;  raise e);
clasohm@0
   472
clasohm@0
   473
end;
paulson@1500
   474
paulson@1500
   475
open Goals;