src/Pure/Isar/isar_cmd.ML
author wenzelm
Fri May 21 16:23:48 1999 +0200 (1999-05-21 ago)
changeset 6694 335833a8b10a
parent 6686 08b06cd19f8d
child 6734 151c07f5b70a
permissions -rw-r--r--
added use_thy_only;
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@5831
    10
  val break: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    11
  val exit: Toplevel.transition -> Toplevel.transition
wenzelm@5991
    12
  val restart: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    13
  val quit: Toplevel.transition -> Toplevel.transition
wenzelm@6686
    14
  val clear_undo: Toplevel.transition -> Toplevel.transition
wenzelm@6686
    15
  val undo: Toplevel.transition -> Toplevel.transition
wenzelm@6686
    16
  val redo: Toplevel.transition -> Toplevel.transition
wenzelm@6686
    17
  val undos: int -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    18
  val use: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    19
  val use_mltext_theory: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    20
  val use_mltext: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    21
  val use_commit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    22
  val cd: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    23
  val pwd: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    24
  val use_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6694
    25
  val use_thy_only: string -> Toplevel.transition -> Toplevel.transition
wenzelm@6195
    26
  val update_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    27
  val print_theory: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    28
  val print_syntax: Toplevel.transition -> Toplevel.transition
wenzelm@5880
    29
  val print_theorems: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    30
  val print_attributes: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    31
  val print_methods: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    32
  val print_binds: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    33
  val print_lthms: Toplevel.transition -> Toplevel.transition
wenzelm@5880
    34
  val print_thms: xstring * Args.src list -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    35
  val print_prop: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    36
  val print_term: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    37
  val print_type: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    38
end;
wenzelm@5831
    39
wenzelm@5831
    40
structure IsarCmd: ISAR_CMD =
wenzelm@5831
    41
struct
wenzelm@5831
    42
wenzelm@5831
    43
wenzelm@5831
    44
(* variations on exit *)
wenzelm@5831
    45
wenzelm@5913
    46
val break = Toplevel.keep (fn state => raise Toplevel.BREAK state);
wenzelm@5831
    47
wenzelm@5831
    48
val exit = Toplevel.keep (fn state =>
wenzelm@5831
    49
  (Context.set_context (try Toplevel.theory_of state);
wenzelm@6635
    50
    writeln "Leaving the Isar loop.  Invoke 'loop();' to restart.";
wenzelm@5831
    51
    raise Toplevel.TERMINATE));
wenzelm@5831
    52
wenzelm@5991
    53
val restart = Toplevel.imperative (fn () => raise Toplevel.RESTART);
wenzelm@5831
    54
val quit = Toplevel.imperative quit;
wenzelm@5831
    55
wenzelm@5831
    56
wenzelm@6686
    57
(* history commands *)
wenzelm@6686
    58
wenzelm@6686
    59
val clear_undo = Toplevel.history History.clear o Toplevel.proof ProofHistory.clear;
wenzelm@6686
    60
val redo = Toplevel.history History.redo o Toplevel.proof ProofHistory.redo;
wenzelm@6686
    61
wenzelm@6686
    62
fun undo_proof prf =
wenzelm@6686
    63
  if ProofHistory.is_initial prf then raise Toplevel.UNDEF else ProofHistory.undo prf;
wenzelm@6686
    64
wenzelm@6686
    65
fun undo_node hist =
wenzelm@6686
    66
  if History.is_initial hist then raise Toplevel.UNDEF else History.undo hist;
wenzelm@6686
    67
wenzelm@6686
    68
val undo = Toplevel.kill o Toplevel.history undo_node o Toplevel.proof undo_proof;
wenzelm@6686
    69
wenzelm@6686
    70
(* FIXME fix *)
wenzelm@6686
    71
fun undos n = Toplevel.history (funpow n History.undo) o Toplevel.proof (funpow n undo_proof);
wenzelm@6686
    72
wenzelm@6686
    73
wenzelm@5831
    74
(* use ML text *)
wenzelm@5831
    75
wenzelm@6264
    76
fun use name = Toplevel.keep (fn state =>
wenzelm@6264
    77
  Context.setmp (try Toplevel.theory_of state) ThyInfo.use name);
wenzelm@5831
    78
wenzelm@5831
    79
(*passes changes of theory context*)
wenzelm@5831
    80
val use_mltext_theory = Toplevel.theory o IsarThy.use_mltext_theory;
wenzelm@5831
    81
wenzelm@5831
    82
(*ignore result theory context*)
wenzelm@5831
    83
fun use_mltext txt = Toplevel.keep (fn state =>
wenzelm@5831
    84
  (IsarThy.use_mltext txt (try Toplevel.theory_of state); ()));
wenzelm@5831
    85
wenzelm@5831
    86
(*Note: commit is dynamically bound*)
wenzelm@5831
    87
val use_commit = use_mltext "commit();";
wenzelm@5831
    88
wenzelm@5831
    89
wenzelm@5831
    90
(* current working directory *)
wenzelm@5831
    91
wenzelm@6228
    92
fun print_cd _ = writeln (Path.pack (File.pwd ()));
wenzelm@5831
    93
wenzelm@6228
    94
fun cd dir = Toplevel.imperative (fn () => (File.cd (Path.unpack dir); print_cd ()));
wenzelm@5831
    95
val pwd = Toplevel.imperative print_cd;
wenzelm@5831
    96
wenzelm@5831
    97
wenzelm@5831
    98
(* load theory files *)
wenzelm@5831
    99
wenzelm@6243
   100
fun use_thy name = Toplevel.imperative (fn () => Context.save ThyInfo.use_thy name);
wenzelm@6694
   101
fun use_thy_only name = Toplevel.imperative (fn () => Context.save ThyInfo.use_thy_only name);
wenzelm@6243
   102
fun update_thy name = Toplevel.imperative (fn () => Context.save ThyInfo.update_thy name);
wenzelm@5831
   103
wenzelm@5831
   104
wenzelm@5831
   105
(* print theory contents *)
wenzelm@5831
   106
wenzelm@5831
   107
val print_theory = Toplevel.keep (PureThy.print_theory o Toplevel.theory_of);
wenzelm@5831
   108
val print_syntax = Toplevel.keep (Display.print_syntax o Toplevel.theory_of);
wenzelm@5880
   109
val print_theorems = Toplevel.keep (PureThy.print_theorems o Toplevel.theory_of);
wenzelm@5831
   110
val print_attributes = Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
wenzelm@5831
   111
val print_methods = Toplevel.keep (Method.print_methods o Toplevel.theory_of);
wenzelm@5831
   112
wenzelm@5831
   113
wenzelm@5831
   114
(* print proof context contents *)
wenzelm@5831
   115
wenzelm@5831
   116
val print_binds = Toplevel.keep (ProofContext.print_binds o Proof.context_of o Toplevel.proof_of);
wenzelm@5831
   117
val print_lthms = Toplevel.keep (ProofContext.print_thms o Proof.context_of o Toplevel.proof_of);
wenzelm@5831
   118
wenzelm@5831
   119
wenzelm@5831
   120
(* print theorems *)
wenzelm@5831
   121
wenzelm@5880
   122
fun global_attribs thy ths srcs =
wenzelm@6091
   123
  #2 (Thm.applys_attributes ((Theory.copy thy, ths), map (Attrib.global_attribute thy) srcs));
wenzelm@5880
   124
wenzelm@5880
   125
fun local_attribs st ths srcs =
wenzelm@6091
   126
  #2 (Thm.applys_attributes ((Proof.context_of st, ths),
wenzelm@5880
   127
    map (Attrib.local_attribute (Proof.theory_of st)) srcs));
wenzelm@5880
   128
wenzelm@5913
   129
fun print_thms (name, srcs) = Toplevel.keep (fn state =>
wenzelm@5831
   130
  let
wenzelm@6091
   131
    val ths = map (Thm.transfer (Toplevel.theory_of state))
wenzelm@6662
   132
      (Toplevel.node_case PureThy.get_thms (ProofContext.get_thms o Proof.context_of)
wenzelm@5831
   133
        state name);
wenzelm@6662
   134
    val ths' = Toplevel.node_case global_attribs local_attribs state ths srcs;
wenzelm@6091
   135
  in Display.print_thms ths' end);
wenzelm@5895
   136
wenzelm@5831
   137
wenzelm@5831
   138
(* print types, terms and props *)
wenzelm@5831
   139
wenzelm@5831
   140
fun read_typ thy = Sign.read_typ (Theory.sign_of thy, K None);
wenzelm@5831
   141
wenzelm@5831
   142
fun read_term T thy s =
wenzelm@5831
   143
  Thm.term_of (#1 (Thm.read_def_cterm (Theory.sign_of thy, K None, K None) [] true (s, T)));
wenzelm@5831
   144
wenzelm@5831
   145
wenzelm@5831
   146
fun print_type s = Toplevel.keep (fn state =>
wenzelm@5831
   147
  let
wenzelm@5831
   148
    val sign = Toplevel.sign_of state;
wenzelm@6662
   149
    val T = Toplevel.node_case read_typ (ProofContext.read_typ o Proof.context_of) state s;
wenzelm@5831
   150
  in Pretty.writeln (Pretty.quote (Sign.pretty_typ sign T)) end);
wenzelm@5831
   151
wenzelm@5831
   152
fun print_term s = Toplevel.keep (fn state =>
wenzelm@5831
   153
  let
wenzelm@5831
   154
    val sign = Toplevel.sign_of state;
wenzelm@6662
   155
    val t = Toplevel.node_case (read_term (TVar (("'z", 0), logicS)))
wenzelm@5831
   156
      (ProofContext.read_term o Proof.context_of) state s;
wenzelm@5831
   157
    val T = Term.type_of t;
wenzelm@5831
   158
  in
wenzelm@5831
   159
    Pretty.writeln (Pretty.block [Pretty.quote (Sign.pretty_term sign t), Pretty.fbrk,
wenzelm@5831
   160
      Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ sign T)])
wenzelm@5831
   161
  end);
wenzelm@5831
   162
wenzelm@5831
   163
fun print_prop s = Toplevel.keep (fn state =>
wenzelm@5831
   164
  let
wenzelm@5831
   165
    val sign = Toplevel.sign_of state;
wenzelm@5831
   166
    val prop =
wenzelm@6662
   167
      Toplevel.node_case (read_term propT) (ProofContext.read_prop o Proof.context_of) state s;
wenzelm@5831
   168
  in Pretty.writeln (Pretty.quote (Sign.pretty_term sign prop)) end);
wenzelm@5831
   169
wenzelm@5831
   170
wenzelm@5831
   171
end;