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