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