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