src/Pure/Isar/isar_cmd.ML
author wenzelm
Fri Sep 03 18:17:51 1999 +0200 (1999-09-03 ago)
changeset 7462 f738df1d82e1
parent 7413 e25ad9ab0b50
child 7503 e8be98558eb8
permissions -rw-r--r--
added welcome;
wenzelm@5831
     1
(*  Title:      Pure/Isar/isar_cmd.ML
wenzelm@5831
     2
    ID:         $Id$
wenzelm@5831
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5831
     4
wenzelm@5831
     5
Non-logical toplevel commands.
wenzelm@5831
     6
*)
wenzelm@5831
     7
wenzelm@5831
     8
signature ISAR_CMD =
wenzelm@5831
     9
sig
wenzelm@7462
    10
  val welcome: Toplevel.transition -> Toplevel.transition
wenzelm@7462
    11
  val init_toplevel: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    12
  val exit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    13
  val quit: Toplevel.transition -> Toplevel.transition
wenzelm@7101
    14
  val touch_all_thys: Toplevel.transition -> Toplevel.transition
wenzelm@7101
    15
  val touch_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7101
    16
  val remove_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6734
    17
  val cannot_undo: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6686
    18
  val clear_undo: Toplevel.transition -> Toplevel.transition
wenzelm@6742
    19
  val redo: Toplevel.transition -> Toplevel.transition
wenzelm@6742
    20
  val undos_proof: int -> Toplevel.transition -> Toplevel.transition
wenzelm@6742
    21
  val kill_proof: Toplevel.transition -> Toplevel.transition
wenzelm@6742
    22
  val undo_theory: Toplevel.transition -> Toplevel.transition
wenzelm@6686
    23
  val undo: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    24
  val use: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    25
  val use_mltext_theory: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    26
  val use_mltext: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    27
  val use_commit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    28
  val cd: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    29
  val pwd: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    30
  val use_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6694
    31
  val use_thy_only: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6195
    32
  val update_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7101
    33
  val update_thy_only: string -> Toplevel.transition -> Toplevel.transition
wenzelm@7124
    34
  val pretty_setmargin: int -> Toplevel.transition -> Toplevel.transition
wenzelm@7308
    35
  val print_context: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    36
  val print_theory: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    37
  val print_syntax: Toplevel.transition -> Toplevel.transition
wenzelm@5880
    38
  val print_theorems: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    39
  val print_attributes: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    40
  val print_methods: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    41
  val print_binds: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    42
  val print_lthms: Toplevel.transition -> Toplevel.transition
wenzelm@7012
    43
  val print_thms: (string * Args.src list) list -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    44
  val print_prop: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    45
  val print_term: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    46
  val print_type: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    47
end;
wenzelm@5831
    48
wenzelm@5831
    49
structure IsarCmd: ISAR_CMD =
wenzelm@5831
    50
struct
wenzelm@5831
    51
wenzelm@5831
    52
wenzelm@7462
    53
(* variations on init / exit *)
wenzelm@7462
    54
wenzelm@7462
    55
val init_toplevel = Toplevel.imperative (fn () => raise Toplevel.RESTART);
wenzelm@7462
    56
val welcome = Toplevel.imperative (writeln o Session.welcome);
wenzelm@5831
    57
wenzelm@5831
    58
val exit = Toplevel.keep (fn state =>
wenzelm@5831
    59
  (Context.set_context (try Toplevel.theory_of state);
wenzelm@6635
    60
    writeln "Leaving the Isar loop.  Invoke 'loop();' to restart.";
wenzelm@5831
    61
    raise Toplevel.TERMINATE));
wenzelm@5831
    62
wenzelm@5831
    63
val quit = Toplevel.imperative quit;
wenzelm@5831
    64
wenzelm@7101
    65
wenzelm@7101
    66
(* touch theories *)
wenzelm@7101
    67
wenzelm@7101
    68
val touch_all_thys = Toplevel.imperative ThyInfo.touch_all_thys;
wenzelm@7101
    69
fun touch_thy name = Toplevel.imperative (fn () => ThyInfo.touch_thy name);
wenzelm@7101
    70
fun remove_thy name = Toplevel.imperative (fn () => ThyInfo.remove_thy name);
wenzelm@7101
    71
wenzelm@5831
    72
wenzelm@6686
    73
(* history commands *)
wenzelm@6686
    74
wenzelm@6734
    75
fun cannot_undo txt = Toplevel.imperative (fn () => error ("Cannot undo " ^ quote txt));
wenzelm@6686
    76
val clear_undo = Toplevel.history History.clear o Toplevel.proof ProofHistory.clear;
wenzelm@6686
    77
val redo = Toplevel.history History.redo o Toplevel.proof ProofHistory.redo;
wenzelm@6686
    78
wenzelm@6742
    79
fun undos_proof n = Toplevel.proof (fn prf =>
wenzelm@6742
    80
  if ProofHistory.is_initial prf then raise Toplevel.UNDEF else funpow n ProofHistory.undo prf);
wenzelm@6686
    81
wenzelm@6742
    82
val kill_proof = Toplevel.history (fn hist =>
wenzelm@6742
    83
  (case History.current hist of
wenzelm@6742
    84
    Toplevel.Theory _ => raise Toplevel.UNDEF
wenzelm@6742
    85
  | Toplevel.Proof _ => History.undo hist));
wenzelm@6686
    86
wenzelm@6742
    87
val undo_theory = Toplevel.history (fn hist =>
wenzelm@6742
    88
  if History.is_initial hist then raise Toplevel.UNDEF else History.undo hist);
wenzelm@6686
    89
wenzelm@7413
    90
val undo = Toplevel.kill o undo_theory o undos_proof 1;
wenzelm@6686
    91
wenzelm@6686
    92
wenzelm@5831
    93
(* use ML text *)
wenzelm@5831
    94
wenzelm@6264
    95
fun use name = Toplevel.keep (fn state =>
wenzelm@6264
    96
  Context.setmp (try Toplevel.theory_of state) ThyInfo.use name);
wenzelm@5831
    97
wenzelm@5831
    98
(*passes changes of theory context*)
wenzelm@5831
    99
val use_mltext_theory = Toplevel.theory o IsarThy.use_mltext_theory;
wenzelm@5831
   100
wenzelm@5831
   101
(*ignore result theory context*)
wenzelm@5831
   102
fun use_mltext txt = Toplevel.keep (fn state =>
wenzelm@5831
   103
  (IsarThy.use_mltext txt (try Toplevel.theory_of state); ()));
wenzelm@5831
   104
wenzelm@5831
   105
(*Note: commit is dynamically bound*)
wenzelm@5831
   106
val use_commit = use_mltext "commit();";
wenzelm@5831
   107
wenzelm@5831
   108
wenzelm@5831
   109
(* current working directory *)
wenzelm@5831
   110
wenzelm@6228
   111
fun print_cd _ = writeln (Path.pack (File.pwd ()));
wenzelm@5831
   112
wenzelm@6228
   113
fun cd dir = Toplevel.imperative (fn () => (File.cd (Path.unpack dir); print_cd ()));
wenzelm@5831
   114
val pwd = Toplevel.imperative print_cd;
wenzelm@5831
   115
wenzelm@5831
   116
wenzelm@5831
   117
(* load theory files *)
wenzelm@5831
   118
wenzelm@6243
   119
fun use_thy name = Toplevel.imperative (fn () => Context.save ThyInfo.use_thy name);
wenzelm@6694
   120
fun use_thy_only name = Toplevel.imperative (fn () => Context.save ThyInfo.use_thy_only name);
wenzelm@6243
   121
fun update_thy name = Toplevel.imperative (fn () => Context.save ThyInfo.update_thy name);
wenzelm@7124
   122
fun update_thy_only name =
wenzelm@7124
   123
  Toplevel.imperative (fn () => Context.save ThyInfo.update_thy_only name);
wenzelm@7124
   124
wenzelm@7124
   125
wenzelm@7124
   126
(* pretty_setmargin *)
wenzelm@7124
   127
wenzelm@7124
   128
fun pretty_setmargin n = Toplevel.imperative (fn () => Pretty.setmargin n);
wenzelm@5831
   129
wenzelm@5831
   130
wenzelm@7308
   131
(* print theory *)
wenzelm@5831
   132
wenzelm@7308
   133
val print_context = Toplevel.keep Toplevel.print_state_context;
wenzelm@5831
   134
val print_theory = Toplevel.keep (PureThy.print_theory o Toplevel.theory_of);
wenzelm@5831
   135
val print_syntax = Toplevel.keep (Display.print_syntax o Toplevel.theory_of);
wenzelm@5880
   136
val print_theorems = Toplevel.keep (PureThy.print_theorems o Toplevel.theory_of);
wenzelm@5831
   137
val print_attributes = Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
wenzelm@5831
   138
val print_methods = Toplevel.keep (Method.print_methods o Toplevel.theory_of);
wenzelm@5831
   139
wenzelm@5831
   140
wenzelm@5831
   141
(* print proof context contents *)
wenzelm@5831
   142
wenzelm@5831
   143
val print_binds = Toplevel.keep (ProofContext.print_binds o Proof.context_of o Toplevel.proof_of);
wenzelm@5831
   144
val print_lthms = Toplevel.keep (ProofContext.print_thms o Proof.context_of o Toplevel.proof_of);
wenzelm@5831
   145
wenzelm@5831
   146
wenzelm@7012
   147
(* print theorems / types / terms / props *)
wenzelm@5880
   148
wenzelm@7012
   149
fun print_thms args = Toplevel.keep (fn state =>
wenzelm@7012
   150
  let val st = Toplevel.node_case Proof.init_state Proof.enter_forward state
wenzelm@7413
   151
  in Pretty.writeln (Proof.pretty_thms (IsarThy.get_thmss args st)) end);
wenzelm@5895
   152
wenzelm@5831
   153
wenzelm@5831
   154
fun read_typ thy = Sign.read_typ (Theory.sign_of thy, K None);
wenzelm@5831
   155
wenzelm@5831
   156
fun read_term T thy s =
wenzelm@5831
   157
  Thm.term_of (#1 (Thm.read_def_cterm (Theory.sign_of thy, K None, K None) [] true (s, T)));
wenzelm@5831
   158
wenzelm@5831
   159
wenzelm@5831
   160
fun print_type s = Toplevel.keep (fn state =>
wenzelm@5831
   161
  let
wenzelm@5831
   162
    val sign = Toplevel.sign_of state;
wenzelm@6662
   163
    val T = Toplevel.node_case read_typ (ProofContext.read_typ o Proof.context_of) state s;
wenzelm@5831
   164
  in Pretty.writeln (Pretty.quote (Sign.pretty_typ sign T)) end);
wenzelm@5831
   165
wenzelm@5831
   166
fun print_term s = Toplevel.keep (fn state =>
wenzelm@5831
   167
  let
wenzelm@5831
   168
    val sign = Toplevel.sign_of state;
wenzelm@6662
   169
    val t = Toplevel.node_case (read_term (TVar (("'z", 0), logicS)))
wenzelm@5831
   170
      (ProofContext.read_term o Proof.context_of) state s;
wenzelm@5831
   171
    val T = Term.type_of t;
wenzelm@5831
   172
  in
wenzelm@5831
   173
    Pretty.writeln (Pretty.block [Pretty.quote (Sign.pretty_term sign t), Pretty.fbrk,
wenzelm@5831
   174
      Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ sign T)])
wenzelm@5831
   175
  end);
wenzelm@5831
   176
wenzelm@5831
   177
fun print_prop s = Toplevel.keep (fn state =>
wenzelm@5831
   178
  let
wenzelm@5831
   179
    val sign = Toplevel.sign_of state;
wenzelm@5831
   180
    val prop =
wenzelm@6662
   181
      Toplevel.node_case (read_term propT) (ProofContext.read_prop o Proof.context_of) state s;
wenzelm@5831
   182
  in Pretty.writeln (Pretty.quote (Sign.pretty_term sign prop)) end);
wenzelm@5831
   183
wenzelm@5831
   184
wenzelm@5831
   185
end;