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