src/Pure/Isar/isar_cmd.ML
author wenzelm
Mon Nov 16 11:04:35 1998 +0100 (1998-11-16 ago)
changeset 5880 feec44106a8e
parent 5831 996361157cfb
child 5895 457b42674b57
permissions -rw-r--r--
add print_theorems;
print_thms: handle attributes;
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@5831
    12
  val quit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    13
  val use: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    14
  val use_mltext_theory: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    15
  val use_mltext: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    16
  val use_commit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    17
  val cd: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    18
  val pwd: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    19
  val use_thy: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    20
  val load: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    21
  val print_theory: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    22
  val print_syntax: Toplevel.transition -> Toplevel.transition
wenzelm@5880
    23
  val print_theorems: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    24
  val print_attributes: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    25
  val print_methods: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    26
  val print_binds: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    27
  val print_lthms: Toplevel.transition -> Toplevel.transition
wenzelm@5880
    28
  val print_thms: xstring * Args.src list -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    29
  val print_prop: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    30
  val print_term: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    31
  val print_type: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    32
end;
wenzelm@5831
    33
wenzelm@5831
    34
structure IsarCmd: ISAR_CMD =
wenzelm@5831
    35
struct
wenzelm@5831
    36
wenzelm@5831
    37
wenzelm@5831
    38
(* variations on exit *)
wenzelm@5831
    39
wenzelm@5831
    40
val break = Toplevel.imperative (fn () => raise Toplevel.BREAK);
wenzelm@5831
    41
wenzelm@5831
    42
val exit = Toplevel.keep (fn state =>
wenzelm@5831
    43
  (Context.set_context (try Toplevel.theory_of state);
wenzelm@5831
    44
    writeln "Leaving the Isar loop.  Invoke 'main_loop();' to restart.";
wenzelm@5831
    45
    raise Toplevel.TERMINATE));
wenzelm@5831
    46
wenzelm@5831
    47
val quit = Toplevel.imperative quit;
wenzelm@5831
    48
wenzelm@5831
    49
wenzelm@5831
    50
(* use ML text *)
wenzelm@5831
    51
wenzelm@5831
    52
fun use name = Toplevel.imperative (fn () => Use.use name);
wenzelm@5831
    53
wenzelm@5831
    54
(*passes changes of theory context*)
wenzelm@5831
    55
val use_mltext_theory = Toplevel.theory o IsarThy.use_mltext_theory;
wenzelm@5831
    56
wenzelm@5831
    57
(*ignore result theory context*)
wenzelm@5831
    58
fun use_mltext txt = Toplevel.keep (fn state =>
wenzelm@5831
    59
  (IsarThy.use_mltext txt (try Toplevel.theory_of state); ()));
wenzelm@5831
    60
wenzelm@5831
    61
(*Note: commit is dynamically bound*)
wenzelm@5831
    62
val use_commit = use_mltext "commit();";
wenzelm@5831
    63
wenzelm@5831
    64
wenzelm@5831
    65
(* current working directory *)
wenzelm@5831
    66
wenzelm@5831
    67
fun print_cd _ = writeln (Library.pwd ());
wenzelm@5831
    68
wenzelm@5831
    69
fun cd dir = Toplevel.imperative (fn () => (Use.cd dir; print_cd ()));
wenzelm@5831
    70
val pwd = Toplevel.imperative print_cd;
wenzelm@5831
    71
wenzelm@5831
    72
wenzelm@5831
    73
(* load theory files *)
wenzelm@5831
    74
wenzelm@5831
    75
fun use_thy name = Toplevel.imperative (fn () => ThyRead.use_thy name);
wenzelm@5831
    76
wenzelm@5831
    77
fun load name =
wenzelm@5831
    78
  Toplevel.imperative (fn () => Toplevel.excursion (OuterSyntax.read_file (name ^ ".thy")));
wenzelm@5831
    79
wenzelm@5831
    80
wenzelm@5831
    81
(* print theory contents *)
wenzelm@5831
    82
wenzelm@5831
    83
val print_theory = Toplevel.keep (PureThy.print_theory o Toplevel.theory_of);
wenzelm@5831
    84
val print_syntax = Toplevel.keep (Display.print_syntax o Toplevel.theory_of);
wenzelm@5880
    85
val print_theorems = Toplevel.keep (PureThy.print_theorems o Toplevel.theory_of);
wenzelm@5831
    86
val print_attributes = Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
wenzelm@5831
    87
val print_methods = Toplevel.keep (Method.print_methods o Toplevel.theory_of);
wenzelm@5831
    88
wenzelm@5831
    89
wenzelm@5831
    90
(* print proof context contents *)
wenzelm@5831
    91
wenzelm@5831
    92
val print_binds = Toplevel.keep (ProofContext.print_binds o Proof.context_of o Toplevel.proof_of);
wenzelm@5831
    93
val print_lthms = Toplevel.keep (ProofContext.print_thms o Proof.context_of o Toplevel.proof_of);
wenzelm@5831
    94
wenzelm@5831
    95
wenzelm@5831
    96
(* print theorems *)
wenzelm@5831
    97
wenzelm@5880
    98
fun global_attribs thy ths srcs =
wenzelm@5880
    99
  #2 (Attribute.applys ((Theory.copy thy, ths), map (Attrib.global_attribute thy) srcs));
wenzelm@5880
   100
wenzelm@5880
   101
fun local_attribs st ths srcs =
wenzelm@5880
   102
  #2 (Attribute.applys ((Proof.context_of st, ths),
wenzelm@5880
   103
    map (Attrib.local_attribute (Proof.theory_of st)) srcs));
wenzelm@5880
   104
wenzelm@5880
   105
fun print_thms (name, srcs) = Toplevel.keep (fn state =>
wenzelm@5831
   106
  let
wenzelm@5831
   107
    val prt_tthm = Attribute.pretty_tthm;
wenzelm@5831
   108
    fun prt_tthms [th] = prt_tthm th
wenzelm@5831
   109
      | prt_tthms ths = Pretty.block (Pretty.fbreaks (map prt_tthm ths));
wenzelm@5831
   110
wenzelm@5880
   111
    val ths = map (apfst (Thm.transfer (Toplevel.theory_of state)))
wenzelm@5831
   112
      (Toplevel.node_cases PureThy.get_tthms (ProofContext.get_tthms o Proof.context_of)
wenzelm@5831
   113
        state name);
wenzelm@5880
   114
    val ths' = Toplevel.node_cases global_attribs local_attribs state ths srcs;
wenzelm@5880
   115
  in Pretty.writeln (prt_tthms ths') end);
wenzelm@5831
   116
wenzelm@5831
   117
wenzelm@5831
   118
(* print types, terms and props *)
wenzelm@5831
   119
wenzelm@5831
   120
fun read_typ thy = Sign.read_typ (Theory.sign_of thy, K None);
wenzelm@5831
   121
wenzelm@5831
   122
fun read_term T thy s =
wenzelm@5831
   123
  Thm.term_of (#1 (Thm.read_def_cterm (Theory.sign_of thy, K None, K None) [] true (s, T)));
wenzelm@5831
   124
wenzelm@5831
   125
wenzelm@5831
   126
fun print_type s = Toplevel.keep (fn state =>
wenzelm@5831
   127
  let
wenzelm@5831
   128
    val sign = Toplevel.sign_of state;
wenzelm@5831
   129
    val T = Toplevel.node_cases read_typ (ProofContext.read_typ o Proof.context_of) state s;
wenzelm@5831
   130
  in Pretty.writeln (Pretty.quote (Sign.pretty_typ sign T)) end);
wenzelm@5831
   131
wenzelm@5831
   132
fun print_term s = Toplevel.keep (fn state =>
wenzelm@5831
   133
  let
wenzelm@5831
   134
    val sign = Toplevel.sign_of state;
wenzelm@5831
   135
    val t = Toplevel.node_cases (read_term (TVar (("'z", 0), logicS)))
wenzelm@5831
   136
      (ProofContext.read_term o Proof.context_of) state s;
wenzelm@5831
   137
    val T = Term.type_of t;
wenzelm@5831
   138
  in
wenzelm@5831
   139
    Pretty.writeln (Pretty.block [Pretty.quote (Sign.pretty_term sign t), Pretty.fbrk,
wenzelm@5831
   140
      Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ sign T)])
wenzelm@5831
   141
  end);
wenzelm@5831
   142
wenzelm@5831
   143
fun print_prop s = Toplevel.keep (fn state =>
wenzelm@5831
   144
  let
wenzelm@5831
   145
    val sign = Toplevel.sign_of state;
wenzelm@5831
   146
    val prop =
wenzelm@5831
   147
      Toplevel.node_cases (read_term propT) (ProofContext.read_prop o Proof.context_of) state s;
wenzelm@5831
   148
  in Pretty.writeln (Pretty.quote (Sign.pretty_term sign prop)) end);
wenzelm@5831
   149
wenzelm@5831
   150
wenzelm@5831
   151
end;