src/Pure/Isar/isar_cmd.ML
author wenzelm
Thu Aug 03 18:44:24 2000 +0200 (2000-08-03 ago)
changeset 9513 8531c18d9181
parent 9454 ea80449107cc
child 9731 3eb72671e5db
permissions -rw-r--r--
unknown_theory/proof/context;
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@9513
   167
(* print parts of theory and proof context *)
wenzelm@5831
   168
wenzelm@7308
   169
val print_context = Toplevel.keep Toplevel.print_state_context;
wenzelm@9513
   170
wenzelm@9513
   171
val print_theory = Toplevel.unknown_theory o
wenzelm@9513
   172
  Toplevel.keep (PureThy.print_theory o Toplevel.theory_of);
wenzelm@9513
   173
wenzelm@9513
   174
val print_syntax = Toplevel.unknown_theory o
wenzelm@9513
   175
  Toplevel.keep (Display.print_syntax o Toplevel.theory_of);
wenzelm@9513
   176
wenzelm@9513
   177
val print_theorems = Toplevel.unknown_theory o
wenzelm@9513
   178
  Toplevel.keep (PureThy.print_theorems o Toplevel.theory_of);
wenzelm@9513
   179
wenzelm@9513
   180
val print_attributes = Toplevel.unknown_theory o
wenzelm@9513
   181
  Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
wenzelm@9513
   182
wenzelm@9513
   183
val print_trans_rules = Toplevel.unknown_context o
wenzelm@9513
   184
  Toplevel.keep (Toplevel.node_case Calculation.print_global_rules
wenzelm@9513
   185
    (Calculation.print_local_rules o Proof.context_of));
wenzelm@9513
   186
wenzelm@9513
   187
val print_methods = Toplevel.unknown_theory o
wenzelm@9513
   188
  Toplevel.keep (Method.print_methods o Toplevel.theory_of);
wenzelm@9513
   189
wenzelm@9219
   190
val print_antiquotations = Toplevel.imperative IsarOutput.print_antiquotations;
wenzelm@5831
   191
wenzelm@9513
   192
fun print_thms_containing cs = Toplevel.unknown_theory o
wenzelm@9513
   193
  Toplevel.keep (fn state => ThmDatabase.print_thms_containing (Toplevel.theory_of state) cs);
wenzelm@7615
   194
wenzelm@9513
   195
fun thm_deps args = Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@9454
   196
  ThmDeps.thm_deps (IsarThy.get_thmss args (Toplevel.enter_forward_proof state)));
wenzelm@9454
   197
wenzelm@5831
   198
wenzelm@5831
   199
(* print proof context contents *)
wenzelm@5831
   200
wenzelm@9513
   201
val print_binds = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@9513
   202
  ProofContext.setmp_verbose
wenzelm@9513
   203
    ProofContext.print_binds (Proof.context_of (Toplevel.proof_of state)));
wenzelm@9513
   204
wenzelm@9513
   205
val print_lthms = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@9513
   206
  ProofContext.setmp_verbose
wenzelm@9513
   207
    ProofContext.print_thms (Proof.context_of (Toplevel.proof_of state)));
wenzelm@9513
   208
wenzelm@9513
   209
val print_cases = Toplevel.unknown_proof o Toplevel.keep (fn state =>
wenzelm@9513
   210
  ProofContext.setmp_verbose
wenzelm@9513
   211
    ProofContext.print_cases (Proof.context_of (Toplevel.proof_of state)));
wenzelm@5831
   212
wenzelm@5831
   213
wenzelm@7012
   214
(* print theorems / types / terms / props *)
wenzelm@5880
   215
wenzelm@9128
   216
fun string_of_thms state ms args = with_modes ms (fn () =>
wenzelm@9128
   217
  Pretty.string_of (Proof.pretty_thms (IsarThy.get_thmss args state)));
wenzelm@5895
   218
wenzelm@9128
   219
fun string_of_prop state ms s =
wenzelm@5831
   220
  let
wenzelm@9128
   221
    val sign = Proof.sign_of state;
wenzelm@9128
   222
    val prop = ProofContext.read_prop (Proof.context_of state) s;
wenzelm@9128
   223
  in with_modes ms (fn () => Pretty.string_of (Pretty.quote (Sign.pretty_term sign prop))) end;
wenzelm@5831
   224
wenzelm@9128
   225
fun string_of_term state ms s =
wenzelm@5831
   226
  let
wenzelm@9128
   227
    val sign = Proof.sign_of state;
wenzelm@9128
   228
    val t = ProofContext.read_term (Proof.context_of state) s;
wenzelm@5831
   229
    val T = Term.type_of t;
wenzelm@5831
   230
  in
wenzelm@9128
   231
    with_modes ms (fn () => Pretty.string_of
wenzelm@8463
   232
      (Pretty.block [Pretty.quote (Sign.pretty_term sign t), Pretty.fbrk,
wenzelm@8463
   233
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ sign T)]))
wenzelm@9128
   234
  end;
wenzelm@5831
   235
wenzelm@9128
   236
fun string_of_type state ms s =
wenzelm@5831
   237
  let
wenzelm@9128
   238
    val sign = Proof.sign_of state;
wenzelm@9128
   239
    val T = ProofContext.read_typ (Proof.context_of state) s;
wenzelm@9128
   240
  in with_modes ms (fn () => Pretty.string_of (Pretty.quote (Sign.pretty_typ sign T))) end;
wenzelm@9128
   241
wenzelm@9128
   242
fun print_item string_of (x, y) = Toplevel.keep (fn state =>
wenzelm@9454
   243
  writeln (string_of (Toplevel.enter_forward_proof state) x y));
wenzelm@9128
   244
wenzelm@9128
   245
val print_thms = print_item string_of_thms;
wenzelm@9128
   246
val print_prop = print_item string_of_prop;
wenzelm@9128
   247
val print_term = print_item string_of_term;
wenzelm@9128
   248
val print_type = print_item string_of_type;
wenzelm@5831
   249
wenzelm@5831
   250
wenzelm@5831
   251
end;