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