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