src/Pure/Isar/isar_cmd.ML
author wenzelm
Fri Mar 17 16:30:03 2000 +0100 (2000-03-17 ago)
changeset 8498 e16d6b54332e
parent 8463 56949c077bd5
child 8807 0046be1769f9
permissions -rw-r--r--
kill: include kill_proof;
wenzelm@5831
     1
(*  Title:      Pure/Isar/isar_cmd.ML
wenzelm@5831
     2
    ID:         $Id$
wenzelm@5831
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5831
     4
wenzelm@5831
     5
Non-logical toplevel commands.
wenzelm@5831
     6
*)
wenzelm@5831
     7
wenzelm@5831
     8
signature ISAR_CMD =
wenzelm@5831
     9
sig
wenzelm@7462
    10
  val welcome: Toplevel.transition -> Toplevel.transition
wenzelm@7462
    11
  val init_toplevel: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    12
  val exit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    13
  val quit: Toplevel.transition -> Toplevel.transition
wenzelm@7908
    14
  val touch_child_thys: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7101
    15
  val touch_all_thys: Toplevel.transition -> Toplevel.transition
wenzelm@7101
    16
  val touch_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7101
    17
  val remove_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7931
    18
  val kill_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@8463
    19
  val pr: string list * int option -> Toplevel.transition -> Toplevel.transition
wenzelm@8453
    20
  val disable_pr: Toplevel.transition -> Toplevel.transition
wenzelm@8453
    21
  val enable_pr: Toplevel.transition -> Toplevel.transition
wenzelm@6734
    22
  val cannot_undo: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7729
    23
  val clear_undos_theory: int -> Toplevel.transition -> Toplevel.transition
wenzelm@6742
    24
  val redo: Toplevel.transition -> Toplevel.transition
wenzelm@6742
    25
  val undos_proof: int -> Toplevel.transition -> Toplevel.transition
wenzelm@7936
    26
  val kill_proof_notify: (unit -> unit) -> Toplevel.transition -> Toplevel.transition
wenzelm@6742
    27
  val kill_proof: Toplevel.transition -> Toplevel.transition
wenzelm@6742
    28
  val undo_theory: Toplevel.transition -> Toplevel.transition
wenzelm@6686
    29
  val undo: Toplevel.transition -> Toplevel.transition
wenzelm@8498
    30
  val kill: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    31
  val use: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    32
  val use_mltext_theory: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7891
    33
  val use_mltext: bool -> string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    34
  val use_commit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    35
  val cd: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    36
  val pwd: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    37
  val use_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6694
    38
  val use_thy_only: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6195
    39
  val update_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7101
    40
  val update_thy_only: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7124
    41
  val pretty_setmargin: int -> Toplevel.transition -> Toplevel.transition
wenzelm@7308
    42
  val print_context: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    43
  val print_theory: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    44
  val print_syntax: Toplevel.transition -> Toplevel.transition
wenzelm@5880
    45
  val print_theorems: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    46
  val print_attributes: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    47
  val print_methods: Toplevel.transition -> Toplevel.transition
wenzelm@7615
    48
  val print_thms_containing: xstring list -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    49
  val print_binds: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    50
  val print_lthms: Toplevel.transition -> Toplevel.transition
wenzelm@8369
    51
  val print_cases: Toplevel.transition -> Toplevel.transition
wenzelm@8463
    52
  val print_thms: string list * (string * Args.src list) list
wenzelm@8463
    53
    -> Toplevel.transition -> Toplevel.transition
wenzelm@8463
    54
  val print_prop: string list * string -> Toplevel.transition -> Toplevel.transition
wenzelm@8463
    55
  val print_term: string list * string -> Toplevel.transition -> Toplevel.transition
wenzelm@8463
    56
  val print_type: string list * string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    57
end;
wenzelm@5831
    58
wenzelm@5831
    59
structure IsarCmd: ISAR_CMD =
wenzelm@5831
    60
struct
wenzelm@5831
    61
wenzelm@5831
    62
wenzelm@7462
    63
(* variations on init / exit *)
wenzelm@7462
    64
wenzelm@7462
    65
val init_toplevel = Toplevel.imperative (fn () => raise Toplevel.RESTART);
wenzelm@7462
    66
val welcome = Toplevel.imperative (writeln o Session.welcome);
wenzelm@5831
    67
wenzelm@5831
    68
val exit = Toplevel.keep (fn state =>
wenzelm@5831
    69
  (Context.set_context (try Toplevel.theory_of state);
wenzelm@6635
    70
    writeln "Leaving the Isar loop.  Invoke 'loop();' to restart.";
wenzelm@5831
    71
    raise Toplevel.TERMINATE));
wenzelm@5831
    72
wenzelm@5831
    73
val quit = Toplevel.imperative quit;
wenzelm@5831
    74
wenzelm@7101
    75
wenzelm@7101
    76
(* touch theories *)
wenzelm@7101
    77
wenzelm@7908
    78
fun touch_child_thys name = Toplevel.imperative (fn () => ThyInfo.touch_child_thys name);
wenzelm@7101
    79
val touch_all_thys = Toplevel.imperative ThyInfo.touch_all_thys;
wenzelm@7101
    80
fun touch_thy name = Toplevel.imperative (fn () => ThyInfo.touch_thy name);
wenzelm@7101
    81
fun remove_thy name = Toplevel.imperative (fn () => ThyInfo.remove_thy name);
wenzelm@7931
    82
fun kill_thy name = Toplevel.imperative (fn () => IsarThy.kill_theory name);
wenzelm@7101
    83
wenzelm@5831
    84
wenzelm@8453
    85
(* print state *)
wenzelm@8453
    86
wenzelm@8463
    87
fun with_modes modes e =
wenzelm@8463
    88
  Library.setmp print_mode (modes @ ! print_mode) e ();
wenzelm@8463
    89
wenzelm@8463
    90
fun pr (ms, limit) = Toplevel.keep (fn state =>
wenzelm@8463
    91
  ((case limit of Some n => goals_limit := n | None => ()); Toplevel.quiet := false;
wenzelm@8463
    92
    with_modes ms (fn () => Toplevel.print_state state)));
wenzelm@8453
    93
wenzelm@8453
    94
val disable_pr = Toplevel.imperative (fn () => Toplevel.quiet := true);
wenzelm@8453
    95
val enable_pr = Toplevel.imperative (fn () => Toplevel.quiet := false);
wenzelm@8453
    96
wenzelm@8453
    97
wenzelm@6686
    98
(* history commands *)
wenzelm@6686
    99
wenzelm@6734
   100
fun cannot_undo txt = Toplevel.imperative (fn () => error ("Cannot undo " ^ quote txt));
wenzelm@7729
   101
val clear_undos_theory = Toplevel.history o History.clear;
wenzelm@6686
   102
val redo = Toplevel.history History.redo o Toplevel.proof ProofHistory.redo;
wenzelm@6686
   103
wenzelm@6742
   104
fun undos_proof n = Toplevel.proof (fn prf =>
wenzelm@6742
   105
  if ProofHistory.is_initial prf then raise Toplevel.UNDEF else funpow n ProofHistory.undo prf);
wenzelm@6686
   106
wenzelm@7936
   107
fun kill_proof_notify (f: unit -> unit) = Toplevel.history (fn hist =>
wenzelm@6742
   108
  (case History.current hist of
wenzelm@6742
   109
    Toplevel.Theory _ => raise Toplevel.UNDEF
wenzelm@7936
   110
  | Toplevel.Proof _ => (f (); History.undo hist)));
wenzelm@7936
   111
wenzelm@7936
   112
val kill_proof = kill_proof_notify (K ());
wenzelm@6686
   113
wenzelm@6742
   114
val undo_theory = Toplevel.history (fn hist =>
wenzelm@6742
   115
  if History.is_initial hist then raise Toplevel.UNDEF else History.undo hist);
wenzelm@6686
   116
wenzelm@7413
   117
val undo = Toplevel.kill o undo_theory o undos_proof 1;
wenzelm@8498
   118
val kill = Toplevel.kill o kill_proof;
wenzelm@6686
   119
wenzelm@6686
   120
wenzelm@5831
   121
(* use ML text *)
wenzelm@5831
   122
wenzelm@6264
   123
fun use name = Toplevel.keep (fn state =>
wenzelm@6264
   124
  Context.setmp (try Toplevel.theory_of state) ThyInfo.use name);
wenzelm@5831
   125
wenzelm@5831
   126
(*passes changes of theory context*)
wenzelm@8349
   127
val use_mltext_theory = Toplevel.theory' o Context.use_mltext_theory;
wenzelm@5831
   128
wenzelm@5831
   129
(*ignore result theory context*)
wenzelm@7891
   130
fun use_mltext v txt = Toplevel.keep' (fn verb => fn state =>
wenzelm@8349
   131
  (Context.use_mltext txt (v andalso verb) (try Toplevel.theory_of state)));
wenzelm@5831
   132
wenzelm@5831
   133
(*Note: commit is dynamically bound*)
wenzelm@7891
   134
val use_commit = use_mltext false "commit();";
wenzelm@5831
   135
wenzelm@5831
   136
wenzelm@5831
   137
(* current working directory *)
wenzelm@5831
   138
wenzelm@7790
   139
fun cd dir = Toplevel.imperative (fn () => (File.cd (Path.unpack dir)));
wenzelm@7790
   140
val pwd = Toplevel.imperative (fn () => writeln (Path.pack (File.pwd ())));
wenzelm@5831
   141
wenzelm@5831
   142
wenzelm@5831
   143
(* load theory files *)
wenzelm@5831
   144
wenzelm@6243
   145
fun use_thy name = Toplevel.imperative (fn () => Context.save ThyInfo.use_thy name);
wenzelm@6694
   146
fun use_thy_only name = Toplevel.imperative (fn () => Context.save ThyInfo.use_thy_only name);
wenzelm@6243
   147
fun update_thy name = Toplevel.imperative (fn () => Context.save ThyInfo.update_thy name);
wenzelm@7124
   148
fun update_thy_only name =
wenzelm@7124
   149
  Toplevel.imperative (fn () => Context.save ThyInfo.update_thy_only name);
wenzelm@7124
   150
wenzelm@7124
   151
wenzelm@7124
   152
(* pretty_setmargin *)
wenzelm@7124
   153
wenzelm@7124
   154
fun pretty_setmargin n = Toplevel.imperative (fn () => Pretty.setmargin n);
wenzelm@5831
   155
wenzelm@5831
   156
wenzelm@7308
   157
(* print theory *)
wenzelm@5831
   158
wenzelm@7308
   159
val print_context = Toplevel.keep Toplevel.print_state_context;
wenzelm@5831
   160
val print_theory = Toplevel.keep (PureThy.print_theory o Toplevel.theory_of);
wenzelm@5831
   161
val print_syntax = Toplevel.keep (Display.print_syntax o Toplevel.theory_of);
wenzelm@5880
   162
val print_theorems = Toplevel.keep (PureThy.print_theorems o Toplevel.theory_of);
wenzelm@5831
   163
val print_attributes = Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
wenzelm@5831
   164
val print_methods = Toplevel.keep (Method.print_methods o Toplevel.theory_of);
wenzelm@5831
   165
wenzelm@7615
   166
fun print_thms_containing cs = Toplevel.keep (fn state =>
wenzelm@7615
   167
  ThmDatabase.print_thms_containing (Toplevel.theory_of state) cs);
wenzelm@7615
   168
wenzelm@5831
   169
wenzelm@5831
   170
(* print proof context contents *)
wenzelm@5831
   171
wenzelm@5831
   172
val print_binds = Toplevel.keep (ProofContext.print_binds o Proof.context_of o Toplevel.proof_of);
wenzelm@5831
   173
val print_lthms = Toplevel.keep (ProofContext.print_thms o Proof.context_of o Toplevel.proof_of);
wenzelm@8369
   174
val print_cases = Toplevel.keep (ProofContext.print_cases o Proof.context_of o Toplevel.proof_of);
wenzelm@5831
   175
wenzelm@5831
   176
wenzelm@7012
   177
(* print theorems / types / terms / props *)
wenzelm@5880
   178
wenzelm@8463
   179
fun print_thms (ms, args) = Toplevel.keep (fn state =>
wenzelm@8498
   180
  let
wenzelm@8498
   181
    val st = Toplevel.node_case Proof.init_state Proof.enter_forward state;
wenzelm@8498
   182
    val transfer = Thm.transfer (Toplevel.theory_of state);
wenzelm@8498
   183
  in
wenzelm@8498
   184
    with_modes ms (fn () =>
wenzelm@8498
   185
      Pretty.writeln (Proof.pretty_thms (map transfer (IsarThy.get_thmss args st))))
wenzelm@8498
   186
  end);
wenzelm@5895
   187
wenzelm@8463
   188
fun print_type (ms, s) = Toplevel.keep (fn state =>
wenzelm@5831
   189
  let
wenzelm@5831
   190
    val sign = Toplevel.sign_of state;
wenzelm@7503
   191
    val T = ProofContext.read_typ (Toplevel.context_of state) s;
wenzelm@8463
   192
  in with_modes ms (fn () => Pretty.writeln (Pretty.quote (Sign.pretty_typ sign T))) end);
wenzelm@5831
   193
wenzelm@8463
   194
fun print_term (ms, s) = Toplevel.keep (fn state =>
wenzelm@5831
   195
  let
wenzelm@5831
   196
    val sign = Toplevel.sign_of state;
wenzelm@7503
   197
    val t = ProofContext.read_term (Toplevel.context_of state) s;
wenzelm@5831
   198
    val T = Term.type_of t;
wenzelm@5831
   199
  in
wenzelm@8463
   200
    with_modes ms (fn () => Pretty.writeln
wenzelm@8463
   201
      (Pretty.block [Pretty.quote (Sign.pretty_term sign t), Pretty.fbrk,
wenzelm@8463
   202
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ sign T)]))
wenzelm@5831
   203
  end);
wenzelm@5831
   204
wenzelm@8463
   205
fun print_prop (ms, s) = Toplevel.keep (fn state =>
wenzelm@5831
   206
  let
wenzelm@5831
   207
    val sign = Toplevel.sign_of state;
wenzelm@7503
   208
    val prop = ProofContext.read_prop (Toplevel.context_of state) s;
wenzelm@8463
   209
  in with_modes ms (fn () => Pretty.writeln (Pretty.quote (Sign.pretty_term sign prop))) end);
wenzelm@5831
   210
wenzelm@5831
   211
wenzelm@5831
   212
end;