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