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