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