src/Pure/Isar/isar_cmd.ML
author wenzelm
Mon Jul 14 11:19:39 2008 +0200 (2008-07-14 ago)
changeset 27562 bcb01eb565ee
parent 27493 f05b6944319c
child 27574 4adce8310643
permissions -rw-r--r--
removed obsolete history commands;
moved back, cannot_undo to isar_syn.ML;
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@21350
     5
Derived Isar commands.
wenzelm@5831
     6
*)
wenzelm@5831
     7
wenzelm@5831
     8
signature ISAR_CMD =
wenzelm@5831
     9
sig
wenzelm@26435
    10
  val generic_setup: string * Position.T -> theory -> theory
wenzelm@26385
    11
  val parse_ast_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    12
  val parse_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    13
  val print_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    14
  val typed_print_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    15
  val print_ast_translation: bool * (string * Position.T) -> theory -> theory
wenzelm@26385
    16
  val oracle: bstring -> string -> string * Position.T -> theory -> theory
wenzelm@21350
    17
  val add_axioms: ((bstring * string) * Attrib.src list) list -> theory -> theory
wenzelm@21350
    18
  val add_defs: (bool * bool) * ((bstring * string) * Attrib.src list) list -> theory -> theory
wenzelm@26336
    19
  val apply_theorems: (Facts.ref * Attrib.src list) list -> theory -> thm list * theory
wenzelm@21350
    20
  val apply_theorems_i: (thm list * attribute list) list -> theory -> thm list * theory
wenzelm@26385
    21
  val declaration: string * Position.T -> local_theory -> local_theory
wenzelm@26385
    22
  val simproc_setup: string -> string list -> string * Position.T -> string list ->
wenzelm@26385
    23
    local_theory -> local_theory
wenzelm@26671
    24
  val hide_names: bool -> string * xstring list -> theory -> theory
wenzelm@21350
    25
  val have: ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@21350
    26
    bool -> Proof.state -> Proof.state
wenzelm@21350
    27
  val hence: ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@21350
    28
    bool -> Proof.state -> Proof.state
wenzelm@21350
    29
  val show: ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@21350
    30
    bool -> Proof.state -> Proof.state
wenzelm@21350
    31
  val thus: ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@21350
    32
    bool -> Proof.state -> Proof.state
wenzelm@21350
    33
  val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
wenzelm@21350
    34
  val terminal_proof: Method.text * Method.text option ->
wenzelm@21350
    35
    Toplevel.transition -> Toplevel.transition
wenzelm@21350
    36
  val default_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    37
  val immediate_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    38
  val done_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    39
  val skip_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    40
  val begin_theory: string -> string list -> (string * bool) list -> bool -> theory
wenzelm@21350
    41
  val end_theory: theory -> theory
wenzelm@21350
    42
  val theory: string * string list * (string * bool) list
wenzelm@21350
    43
    -> Toplevel.transition -> Toplevel.transition
wenzelm@7462
    44
  val welcome: Toplevel.transition -> Toplevel.transition
wenzelm@7462
    45
  val init_toplevel: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    46
  val exit: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    47
  val quit: Toplevel.transition -> Toplevel.transition
wenzelm@9731
    48
  val pr: string list * (int option * int option) -> Toplevel.transition -> Toplevel.transition
wenzelm@8453
    49
  val disable_pr: Toplevel.transition -> Toplevel.transition
wenzelm@8453
    50
  val enable_pr: Toplevel.transition -> Toplevel.transition
wenzelm@26489
    51
  val ml_diag: bool -> string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@25793
    52
  val nested_command: Markup.property list -> string * Position.T -> Toplevel.transition
wenzelm@14950
    53
  val cd: Path.T -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    54
  val pwd: Toplevel.transition -> Toplevel.transition
wenzelm@14950
    55
  val display_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
wenzelm@14950
    56
  val print_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
wenzelm@7124
    57
  val pretty_setmargin: int -> Toplevel.transition -> Toplevel.transition
wenzelm@7308
    58
  val print_context: Toplevel.transition -> Toplevel.transition
wenzelm@20621
    59
  val print_theory: bool -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    60
  val print_syntax: Toplevel.transition -> Toplevel.transition
wenzelm@21725
    61
  val print_abbrevs: Toplevel.transition -> Toplevel.transition
wenzelm@21003
    62
  val print_facts: Toplevel.transition -> Toplevel.transition
wenzelm@24115
    63
  val print_configs: Toplevel.transition -> Toplevel.transition
wenzelm@5880
    64
  val print_theorems: Toplevel.transition -> Toplevel.transition
wenzelm@12060
    65
  val print_locales: Toplevel.transition -> Toplevel.transition
wenzelm@18135
    66
  val print_locale: bool * (Locale.expr * Element.context list)
wenzelm@12758
    67
    -> Toplevel.transition -> Toplevel.transition
ballarin@17139
    68
  val print_registrations: bool -> string -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    69
  val print_attributes: Toplevel.transition -> Toplevel.transition
wenzelm@16026
    70
  val print_simpset: Toplevel.transition -> Toplevel.transition
wenzelm@12382
    71
  val print_rules: Toplevel.transition -> Toplevel.transition
wenzelm@9219
    72
  val print_trans_rules: Toplevel.transition -> Toplevel.transition
wenzelm@5831
    73
  val print_methods: Toplevel.transition -> Toplevel.transition
wenzelm@9219
    74
  val print_antiquotations: Toplevel.transition -> Toplevel.transition
wenzelm@20574
    75
  val class_deps: Toplevel.transition -> Toplevel.transition
haftmann@22485
    76
  val thy_deps: Toplevel.transition -> Toplevel.transition
wenzelm@26336
    77
  val thm_deps: (Facts.ref * Attrib.src list) list -> Toplevel.transition -> Toplevel.transition
kleing@22340
    78
  val find_theorems: (int option * bool) * (bool * string FindTheorems.criterion) list
wenzelm@13284
    79
    -> Toplevel.transition -> Toplevel.transition
berghofe@26184
    80
  val unused_thms: (string list * string list option) option ->
berghofe@26184
    81
    Toplevel.transition -> Toplevel.transition
wenzelm@5831
    82
  val print_binds: Toplevel.transition -> Toplevel.transition
wenzelm@8369
    83
  val print_cases: Toplevel.transition -> Toplevel.transition
wenzelm@26336
    84
  val print_stmts: string list * (Facts.ref * Attrib.src list) list
wenzelm@19268
    85
    -> Toplevel.transition -> Toplevel.transition
wenzelm@26336
    86
  val print_thms: string list * (Facts.ref * Attrib.src list) list
wenzelm@10581
    87
    -> Toplevel.transition -> Toplevel.transition
wenzelm@26336
    88
  val print_prfs: bool -> string list * (Facts.ref * Attrib.src list) list option
berghofe@11524
    89
    -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    90
  val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    91
  val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    92
  val print_type: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@12938
    93
  val add_header: string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@17262
    94
  val add_chapter: xstring option * (string * Position.T) ->
wenzelm@17262
    95
    Toplevel.transition -> Toplevel.transition
wenzelm@17262
    96
  val add_section: xstring option * (string * Position.T) ->
wenzelm@17262
    97
    Toplevel.transition -> Toplevel.transition
wenzelm@17262
    98
  val add_subsection: xstring option * (string * Position.T) ->
wenzelm@17262
    99
    Toplevel.transition -> Toplevel.transition
wenzelm@17262
   100
  val add_subsubsection: xstring option * (string * Position.T) ->
wenzelm@17262
   101
    Toplevel.transition -> Toplevel.transition
wenzelm@17262
   102
  val add_text: xstring option * (string * Position.T) ->
wenzelm@17262
   103
    Toplevel.transition -> Toplevel.transition
wenzelm@12938
   104
  val add_text_raw: string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@12938
   105
  val add_sect: string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@12938
   106
  val add_subsect: string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@12938
   107
  val add_subsubsect: string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@12938
   108
  val add_txt: string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@12938
   109
  val add_txt_raw: string * Position.T -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
   110
end;
wenzelm@5831
   111
wenzelm@5831
   112
structure IsarCmd: ISAR_CMD =
wenzelm@5831
   113
struct
wenzelm@5831
   114
wenzelm@5831
   115
wenzelm@22116
   116
(** theory declarations **)
wenzelm@22116
   117
wenzelm@22116
   118
(* generic_setup *)
wenzelm@22116
   119
wenzelm@26435
   120
fun generic_setup (txt, pos) =
wenzelm@26455
   121
  ML_Context.expression pos "val setup: theory -> theory" "Context.map_theory setup" txt
wenzelm@26435
   122
  |> Context.theory_map;
wenzelm@22116
   123
wenzelm@22116
   124
wenzelm@22116
   125
(* translation functions *)
wenzelm@22116
   126
wenzelm@22116
   127
local
wenzelm@22116
   128
wenzelm@22116
   129
fun advancedT false = ""
wenzelm@22116
   130
  | advancedT true = "Proof.context -> ";
wenzelm@22116
   131
wenzelm@22116
   132
fun advancedN false = ""
wenzelm@22116
   133
  | advancedN true = "advanced_";
wenzelm@22116
   134
wenzelm@22116
   135
in
wenzelm@22116
   136
wenzelm@26385
   137
fun parse_ast_translation (a, (txt, pos)) =
wenzelm@26455
   138
  txt |> ML_Context.expression pos
wenzelm@26455
   139
    ("val parse_ast_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   140
      "Syntax.ast list -> Syntax.ast)) list")
wenzelm@22116
   141
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns (parse_ast_translation, [], [], []))")
wenzelm@22116
   142
  |> Context.theory_map;
wenzelm@22116
   143
wenzelm@26385
   144
fun parse_translation (a, (txt, pos)) =
wenzelm@26455
   145
  txt |> ML_Context.expression pos
wenzelm@26455
   146
    ("val parse_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   147
      "term list -> term)) list")
wenzelm@22116
   148
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], parse_translation, [], []))")
wenzelm@22116
   149
  |> Context.theory_map;
wenzelm@22116
   150
wenzelm@26385
   151
fun print_translation (a, (txt, pos)) =
wenzelm@26455
   152
  txt |> ML_Context.expression pos
wenzelm@26455
   153
    ("val print_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   154
      "term list -> term)) list")
wenzelm@22116
   155
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], print_translation, []))")
wenzelm@22116
   156
  |> Context.theory_map;
wenzelm@22116
   157
wenzelm@26385
   158
fun print_ast_translation (a, (txt, pos)) =
wenzelm@26455
   159
  txt |> ML_Context.expression pos
wenzelm@26455
   160
    ("val print_ast_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   161
      "Syntax.ast list -> Syntax.ast)) list")
wenzelm@22116
   162
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], [], print_ast_translation))")
wenzelm@22116
   163
  |> Context.theory_map;
wenzelm@22116
   164
wenzelm@26385
   165
fun typed_print_translation (a, (txt, pos)) =
wenzelm@26455
   166
  txt |> ML_Context.expression pos
wenzelm@26455
   167
    ("val typed_print_translation: (string * (" ^ advancedT a ^
wenzelm@22116
   168
      "bool -> typ -> term list -> term)) list")
wenzelm@22116
   169
    ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfunsT typed_print_translation)")
wenzelm@22116
   170
  |> Context.theory_map;
wenzelm@22116
   171
wenzelm@22116
   172
end;
wenzelm@22116
   173
wenzelm@22116
   174
wenzelm@22116
   175
(* oracles *)
wenzelm@22116
   176
wenzelm@26385
   177
fun oracle name typ (oracle, pos) =
wenzelm@22116
   178
  let val txt =
wenzelm@26385
   179
    "local\
wenzelm@26385
   180
    \  type T = " ^ typ ^ ";\
wenzelm@22116
   181
    \  val oracle: theory -> T -> term = " ^ oracle ^ ";\n\
wenzelm@22116
   182
    \  val name = " ^ quote name ^ ";\n\
wenzelm@22116
   183
    \  exception Arg of T;\n\
wenzelm@26463
   184
    \  val _ = Context.>> (Context.map_theory\n\
wenzelm@26463
   185
    \    (Theory.add_oracle (name, fn (thy, Arg x) => oracle thy x)));\n\
wenzelm@26425
   186
    \  val thy = ML_Context.the_global_context ();\n\
wenzelm@22116
   187
    \  val invoke_" ^ name ^ " = Thm.invoke_oracle_i thy (Sign.full_name thy name);\n\
wenzelm@22116
   188
    \in\n\
wenzelm@22116
   189
    \  fun " ^ name ^ " thy x = invoke_" ^ name ^ " (thy, Arg x);\n\
wenzelm@22116
   190
    \end;\n";
wenzelm@26455
   191
  in Context.theory_map (ML_Context.exec (fn () => ML_Context.eval false pos txt)) end;
wenzelm@22116
   192
wenzelm@22116
   193
wenzelm@21350
   194
(* axioms *)
wenzelm@21350
   195
wenzelm@21350
   196
fun add_axms f args thy =
wenzelm@21350
   197
  f (map (fn (x, srcs) => (x, map (Attrib.attribute thy) srcs)) args) thy;
wenzelm@21350
   198
wenzelm@21350
   199
val add_axioms = add_axms (snd oo PureThy.add_axioms);
wenzelm@21350
   200
wenzelm@21350
   201
fun add_defs ((unchecked, overloaded), args) =
wenzelm@21350
   202
  add_axms
wenzelm@21350
   203
    (snd oo (if unchecked then PureThy.add_defs_unchecked else PureThy.add_defs) overloaded) args;
wenzelm@21350
   204
wenzelm@21350
   205
wenzelm@21350
   206
(* facts *)
wenzelm@21350
   207
wenzelm@21350
   208
fun apply_theorems args thy =
wenzelm@21350
   209
  let val facts = Attrib.map_facts (Attrib.attribute thy) [(("", []), args)]
wenzelm@21350
   210
  in apfst (maps snd) (PureThy.note_thmss "" facts thy) end;
wenzelm@21350
   211
wenzelm@21350
   212
fun apply_theorems_i args = apfst (maps snd) o PureThy.note_thmss_i "" [(("", []), args)];
wenzelm@21350
   213
wenzelm@21350
   214
wenzelm@22087
   215
(* declarations *)
wenzelm@22087
   216
wenzelm@26385
   217
fun declaration (txt, pos) =
wenzelm@26455
   218
  txt |> ML_Context.expression pos
wenzelm@26455
   219
    "val declaration: Morphism.declaration"
wenzelm@24020
   220
    "Context.map_proof (LocalTheory.declaration declaration)"
wenzelm@26385
   221
  |> Context.proof_map;
wenzelm@22087
   222
wenzelm@22087
   223
wenzelm@22202
   224
(* simprocs *)
wenzelm@22202
   225
wenzelm@26385
   226
fun simproc_setup name lhss (proc, pos) identifier =
wenzelm@26455
   227
  ML_Context.expression pos
wenzelm@22239
   228
    "val proc: Morphism.morphism -> Simplifier.simpset -> cterm -> thm option"
wenzelm@22239
   229
  ("Context.map_proof (Simplifier.def_simproc {name = " ^ ML_Syntax.print_string name ^ ", \
wenzelm@22239
   230
    \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
wenzelm@22239
   231
    \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})") proc
wenzelm@22202
   232
  |> Context.proof_map;
wenzelm@22202
   233
wenzelm@22202
   234
wenzelm@26671
   235
(* hide names *)
wenzelm@26671
   236
wenzelm@26671
   237
val hide_kinds =
wenzelm@26671
   238
 [("class", (Sign.intern_class, can o Sign.certify_class, Sign.hide_class)),
wenzelm@26671
   239
  ("type", (Sign.intern_type, Sign.declared_tyname, Sign.hide_type)),
wenzelm@26671
   240
  ("const", (Sign.intern_const, Sign.declared_const, Sign.hide_const)),
wenzelm@26694
   241
  ("fact", (PureThy.intern_fact, PureThy.defined_fact, PureThy.hide_fact))];
wenzelm@26671
   242
wenzelm@26671
   243
fun hide_names b (kind, xnames) thy =
wenzelm@26671
   244
  (case AList.lookup (op =) hide_kinds kind of
wenzelm@26671
   245
    SOME (intern, check, hide) =>
wenzelm@26671
   246
      let
wenzelm@26671
   247
        val names = map (intern thy) xnames;
wenzelm@26671
   248
        val bads = filter_out (check thy) names;
wenzelm@26671
   249
      in
wenzelm@26671
   250
        if null bads then fold (hide b) names thy
wenzelm@26671
   251
        else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
wenzelm@26671
   252
      end
wenzelm@26671
   253
  | NONE => error ("Bad name space specification: " ^ quote kind));
wenzelm@26671
   254
wenzelm@26671
   255
wenzelm@21350
   256
(* goals *)
wenzelm@21350
   257
wenzelm@21350
   258
fun goal opt_chain goal stmt int =
wenzelm@21350
   259
  opt_chain #> goal NONE (K Seq.single) stmt int;
wenzelm@21350
   260
wenzelm@21350
   261
val have = goal I Proof.have;
wenzelm@21350
   262
val hence = goal Proof.chain Proof.have;
wenzelm@21350
   263
val show = goal I Proof.show;
wenzelm@21350
   264
val thus = goal Proof.chain Proof.show;
wenzelm@21350
   265
wenzelm@21350
   266
wenzelm@21350
   267
(* local endings *)
wenzelm@21350
   268
wenzelm@21350
   269
fun local_qed m = Toplevel.proofs (Proof.local_qed (m, true));
wenzelm@21350
   270
val local_terminal_proof = Toplevel.proofs o Proof.local_terminal_proof;
wenzelm@21350
   271
val local_default_proof = Toplevel.proofs Proof.local_default_proof;
wenzelm@21350
   272
val local_immediate_proof = Toplevel.proofs Proof.local_immediate_proof;
wenzelm@21350
   273
val local_done_proof = Toplevel.proofs Proof.local_done_proof;
wenzelm@21350
   274
val local_skip_proof = Toplevel.proofs' Proof.local_skip_proof;
wenzelm@21350
   275
wenzelm@27562
   276
val skip_local_qed = Toplevel.skip_proof (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF);
wenzelm@21350
   277
wenzelm@21350
   278
wenzelm@21350
   279
(* global endings *)
wenzelm@21350
   280
wenzelm@21350
   281
fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
wenzelm@21350
   282
val global_terminal_proof = Toplevel.end_proof o K o Proof.global_terminal_proof;
wenzelm@21350
   283
val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
wenzelm@21350
   284
val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
wenzelm@21350
   285
val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
wenzelm@21350
   286
val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
wenzelm@21350
   287
wenzelm@21350
   288
val skip_global_qed = Toplevel.skip_proof_to_theory (equal 1);
wenzelm@21350
   289
wenzelm@21350
   290
wenzelm@21350
   291
(* common endings *)
wenzelm@21350
   292
wenzelm@21350
   293
fun qed m = local_qed m o global_qed m o skip_local_qed o skip_global_qed;
wenzelm@21350
   294
fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
wenzelm@21350
   295
val default_proof = local_default_proof o global_default_proof;
wenzelm@21350
   296
val immediate_proof = local_immediate_proof o global_immediate_proof;
wenzelm@21350
   297
val done_proof = local_done_proof o global_done_proof;
wenzelm@21350
   298
val skip_proof = local_skip_proof o global_skip_proof;
wenzelm@21350
   299
wenzelm@21350
   300
wenzelm@21350
   301
(* init and exit *)
wenzelm@21350
   302
wenzelm@21350
   303
fun begin_theory name imports uses =
wenzelm@23897
   304
  ThyInfo.begin_theory name imports (map (apfst Path.explode) uses);
wenzelm@21350
   305
wenzelm@21566
   306
fun end_theory thy =
wenzelm@21566
   307
  if ThyInfo.check_known_thy (Context.theory_name thy) then ThyInfo.end_theory thy else thy;
wenzelm@21350
   308
wenzelm@21350
   309
fun theory (name, imports, uses) =
wenzelm@27439
   310
  Toplevel.init_theory name (begin_theory name imports uses)
wenzelm@21350
   311
    (fn thy => (end_theory thy; ()))
wenzelm@27493
   312
    (ThyInfo.kill_thy o Context.theory_name);
wenzelm@21350
   313
wenzelm@7462
   314
val init_toplevel = Toplevel.imperative (fn () => raise Toplevel.RESTART);
wenzelm@21350
   315
wenzelm@7462
   316
val welcome = Toplevel.imperative (writeln o Session.welcome);
wenzelm@5831
   317
wenzelm@5831
   318
val exit = Toplevel.keep (fn state =>
wenzelm@26599
   319
 (Context.set_thread_data (try Toplevel.generic_theory_of state);
wenzelm@24071
   320
  raise Toplevel.TERMINATE));
wenzelm@5831
   321
wenzelm@5831
   322
val quit = Toplevel.imperative quit;
wenzelm@5831
   323
wenzelm@7101
   324
wenzelm@8453
   325
(* print state *)
wenzelm@8453
   326
skalberg@15531
   327
fun set_limit _ NONE = ()
skalberg@15531
   328
  | set_limit r (SOME n) = r := n;
wenzelm@9731
   329
wenzelm@19385
   330
fun pr (modes, (lim1, lim2)) = Toplevel.keep (fn state =>
wenzelm@9731
   331
  (set_limit goals_limit lim1; set_limit ProofContext.prems_limit lim2; Toplevel.quiet := false;
wenzelm@23935
   332
    PrintMode.with_modes modes (Toplevel.print_state true) state));
wenzelm@8453
   333
wenzelm@8453
   334
val disable_pr = Toplevel.imperative (fn () => Toplevel.quiet := true);
wenzelm@8453
   335
val enable_pr = Toplevel.imperative (fn () => Toplevel.quiet := false);
wenzelm@8453
   336
wenzelm@8453
   337
wenzelm@26489
   338
(* diagnostic ML evaluation *)
wenzelm@5831
   339
wenzelm@26489
   340
fun ml_diag verbose (txt, pos) = Toplevel.keep (fn state =>
wenzelm@26489
   341
  (ML_Context.eval_in (try Toplevel.generic_theory_of state) verbose pos txt));
wenzelm@5831
   342
wenzelm@5831
   343
wenzelm@25793
   344
(* nested commands *)
wenzelm@25793
   345
wenzelm@25793
   346
fun nested_command props (str, pos) =
wenzelm@26053
   347
  let val pos' = Position.of_properties (props |> Position.default_properties pos) in
wenzelm@25793
   348
    (case OuterSyntax.parse pos' str of
wenzelm@25956
   349
      [transition] => transition
wenzelm@25793
   350
    | _ => error "exactly one command expected")
wenzelm@25793
   351
  end;
wenzelm@25793
   352
wenzelm@25793
   353
wenzelm@5831
   354
(* current working directory *)
wenzelm@5831
   355
wenzelm@14950
   356
fun cd path = Toplevel.imperative (fn () => (File.cd path));
wenzelm@21858
   357
val pwd = Toplevel.imperative (fn () => writeln (Path.implode (File.pwd ())));
wenzelm@5831
   358
wenzelm@5831
   359
wenzelm@14934
   360
(* present draft files *)
wenzelm@14934
   361
wenzelm@14934
   362
fun display_drafts files = Toplevel.imperative (fn () =>
wenzelm@16258
   363
  let val outfile = File.shell_path (Present.drafts (getenv "ISABELLE_DOC_FORMAT") files)
wenzelm@16258
   364
  in File.isatool ("display -c " ^ outfile ^ " &"); () end);
wenzelm@14934
   365
wenzelm@14934
   366
fun print_drafts files = Toplevel.imperative (fn () =>
wenzelm@16258
   367
  let val outfile = File.shell_path (Present.drafts "ps" files)
wenzelm@16258
   368
  in File.isatool ("print -c " ^ outfile); () end);
wenzelm@14934
   369
wenzelm@14934
   370
wenzelm@7124
   371
(* pretty_setmargin *)
wenzelm@7124
   372
wenzelm@7124
   373
fun pretty_setmargin n = Toplevel.imperative (fn () => Pretty.setmargin n);
wenzelm@5831
   374
wenzelm@5831
   375
wenzelm@9513
   376
(* print parts of theory and proof context *)
wenzelm@5831
   377
wenzelm@7308
   378
val print_context = Toplevel.keep Toplevel.print_state_context;
wenzelm@9513
   379
wenzelm@20621
   380
fun print_theory verbose = Toplevel.unknown_theory o
wenzelm@22872
   381
  Toplevel.keep (Pretty.writeln o ProofDisplay.pretty_full_theory verbose o Toplevel.theory_of);
wenzelm@9513
   382
wenzelm@21663
   383
val print_syntax = Toplevel.unknown_context o
wenzelm@21663
   384
  Toplevel.keep (ProofContext.print_syntax o Toplevel.context_of);
wenzelm@9513
   385
wenzelm@21725
   386
val print_abbrevs = Toplevel.unknown_context o
wenzelm@21725
   387
  Toplevel.keep (ProofContext.print_abbrevs o Toplevel.context_of);
wenzelm@21725
   388
wenzelm@21003
   389
val print_facts = Toplevel.unknown_context o Toplevel.keep (fn state =>
wenzelm@21003
   390
  ProofContext.setmp_verbose
wenzelm@21506
   391
    ProofContext.print_lthms (Toplevel.context_of state));
wenzelm@21003
   392
wenzelm@24115
   393
val print_configs = Toplevel.unknown_context o Toplevel.keep (fn state =>
wenzelm@24115
   394
  Attrib.print_configs (Toplevel.context_of state));
wenzelm@23989
   395
wenzelm@17066
   396
val print_theorems_proof = Toplevel.keep (fn state =>
wenzelm@17066
   397
  ProofContext.setmp_verbose
wenzelm@17066
   398
    ProofContext.print_lthms (Proof.context_of (Toplevel.proof_of state)));
wenzelm@17066
   399
wenzelm@18588
   400
val print_theorems_theory = Toplevel.keep (fn state =>
wenzelm@18588
   401
  Toplevel.theory_of state |>
wenzelm@18588
   402
  (case Option.map Toplevel.theory_node (History.previous (Toplevel.node_history_of state)) of
wenzelm@20957
   403
    SOME (SOME prev_thy) => ProofDisplay.print_theorems_diff (Context.theory_of prev_thy)
wenzelm@19430
   404
  | _ => ProofDisplay.print_theorems));
wenzelm@18588
   405
wenzelm@21663
   406
val print_theorems = Toplevel.unknown_context o print_theorems_theory o print_theorems_proof;
wenzelm@9513
   407
wenzelm@12060
   408
val print_locales = Toplevel.unknown_theory o
wenzelm@12060
   409
  Toplevel.keep (Locale.print_locales o Toplevel.theory_of);
wenzelm@12060
   410
wenzelm@22573
   411
fun print_locale (show_facts, (imports, body)) = Toplevel.unknown_theory o
wenzelm@19385
   412
  Toplevel.keep (fn state =>
wenzelm@22573
   413
    Locale.print_locale (Toplevel.theory_of state) show_facts imports body);
ballarin@15596
   414
ballarin@17139
   415
fun print_registrations show_wits name = Toplevel.unknown_context o
wenzelm@20957
   416
  Toplevel.keep (Toplevel.node_case
ballarin@24789
   417
      (Context.cases (Locale.print_registrations show_wits name o ProofContext.init)
ballarin@24789
   418
        (Locale.print_registrations show_wits name))
ballarin@24789
   419
    (Locale.print_registrations show_wits name o Proof.context_of));
wenzelm@12060
   420
wenzelm@9513
   421
val print_attributes = Toplevel.unknown_theory o
wenzelm@9513
   422
  Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
wenzelm@9513
   423
wenzelm@16026
   424
val print_simpset = Toplevel.unknown_context o
wenzelm@26498
   425
  Toplevel.keep (Simplifier.print_ss o Simplifier.local_simpset_of o Toplevel.context_of);
wenzelm@16026
   426
wenzelm@12382
   427
val print_rules = Toplevel.unknown_context o
wenzelm@18639
   428
  Toplevel.keep (ContextRules.print_rules o Toplevel.context_of);
wenzelm@12382
   429
wenzelm@9513
   430
val print_trans_rules = Toplevel.unknown_context o
wenzelm@18639
   431
  Toplevel.keep (Calculation.print_rules o Toplevel.context_of);
wenzelm@9513
   432
wenzelm@9513
   433
val print_methods = Toplevel.unknown_theory o
wenzelm@9513
   434
  Toplevel.keep (Method.print_methods o Toplevel.theory_of);
wenzelm@9513
   435
wenzelm@22116
   436
val print_antiquotations = Toplevel.imperative ThyOutput.print_antiquotations;
wenzelm@5831
   437
haftmann@22485
   438
val thy_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
haftmann@22485
   439
  let
haftmann@22485
   440
    val thy = Toplevel.theory_of state;
wenzelm@24560
   441
    val all_thys = sort ThyInfo.thy_ord (thy :: Theory.ancestors_of thy);
wenzelm@22604
   442
    val gr = all_thys |> map (fn node =>
wenzelm@22602
   443
      let
wenzelm@22602
   444
        val name = Context.theory_name node;
wenzelm@22602
   445
        val parents = map Context.theory_name (Theory.parents_of node);
wenzelm@24560
   446
        val dir = Present.session_name node;
wenzelm@24560
   447
        val unfold = not (ThyInfo.known_thy name andalso ThyInfo.is_finished name);
wenzelm@24560
   448
      in {name = name, ID = name, parents = parents, dir = dir, unfold = unfold, path = ""} end);
wenzelm@22602
   449
  in Present.display_graph gr end);
haftmann@22485
   450
wenzelm@20574
   451
val class_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@20574
   452
  let
wenzelm@20574
   453
    val thy = Toplevel.theory_of state;
wenzelm@20574
   454
    val {classes = (space, algebra), ...} = Type.rep_tsig (Sign.tsig_of thy);
wenzelm@20574
   455
    val {classes, ...} = Sorts.rep_algebra algebra;
wenzelm@20574
   456
    fun entry (c, (i, (_, cs))) =
wenzelm@20574
   457
      (i, {name = NameSpace.extern space c, ID = c, parents = cs,
wenzelm@20574
   458
            dir = "", unfold = true, path = ""});
wenzelm@20574
   459
    val gr =
wenzelm@20574
   460
      Graph.fold (cons o entry) classes []
wenzelm@20574
   461
      |> sort (int_ord o pairself #1) |> map #2;
wenzelm@20574
   462
  in Present.display_graph gr end);
wenzelm@20574
   463
kleing@15964
   464
wenzelm@16026
   465
(* retrieve theorems *)
wenzelm@7615
   466
wenzelm@9513
   467
fun thm_deps args = Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@21003
   468
  ThmDeps.thm_deps (Proof.get_thmss (Toplevel.enter_proof_body state) args));
wenzelm@9454
   469
wenzelm@24560
   470
fun find_theorems ((opt_lim, rem_dups), spec) =
kleing@22340
   471
  Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@16026
   472
  let
wenzelm@21003
   473
    val proof_state = Toplevel.enter_proof_body state;
wenzelm@16026
   474
    val ctxt = Proof.context_of proof_state;
wenzelm@16026
   475
    val opt_goal = try Proof.get_goal proof_state |> Option.map (Thm.prop_of o #2 o #2);
kleing@22340
   476
  in FindTheorems.print_theorems ctxt opt_goal opt_lim rem_dups spec end);
wenzelm@16026
   477
wenzelm@5831
   478
berghofe@26184
   479
(* find unused theorems *)
berghofe@26184
   480
wenzelm@26186
   481
fun unused_thms opt_range = Toplevel.keep (fn state =>
wenzelm@26186
   482
  let
wenzelm@26186
   483
    val thy = Toplevel.theory_of state;
wenzelm@26186
   484
    val ctxt = Toplevel.context_of state;
wenzelm@26186
   485
    fun pretty_thm (a, th) = ProofContext.pretty_fact ctxt (a, [th]);
wenzelm@26186
   486
  in
wenzelm@26186
   487
    ThmDeps.unused_thms
wenzelm@26186
   488
      (case opt_range of
wenzelm@26694
   489
        NONE => (Theory.parents_of thy, [thy])
wenzelm@26694
   490
      | SOME (xs, NONE) => (map ThyInfo.get_theory xs, [thy])
wenzelm@26694
   491
      | SOME (xs, SOME ys) => (map ThyInfo.get_theory xs, map ThyInfo.get_theory ys))
wenzelm@26186
   492
    |> map pretty_thm |> Pretty.chunks |> Pretty.writeln
wenzelm@26186
   493
  end);
berghofe@26184
   494
berghofe@26184
   495
wenzelm@5831
   496
(* print proof context contents *)
wenzelm@5831
   497
wenzelm@21663
   498
val print_binds = Toplevel.unknown_context o Toplevel.keep (fn state =>
wenzelm@21663
   499
  ProofContext.setmp_verbose ProofContext.print_binds (Toplevel.context_of state));
wenzelm@9513
   500
wenzelm@21663
   501
val print_cases = Toplevel.unknown_context o Toplevel.keep (fn state =>
wenzelm@21663
   502
  ProofContext.setmp_verbose ProofContext.print_cases (Toplevel.context_of state));
wenzelm@5831
   503
wenzelm@5831
   504
wenzelm@19268
   505
(* print theorems, terms, types etc. *)
wenzelm@19268
   506
wenzelm@19385
   507
local
wenzelm@19385
   508
wenzelm@19385
   509
fun string_of_stmts state args =
wenzelm@19268
   510
  Proof.get_thmss state args
wenzelm@21437
   511
  |> map (Element.pretty_statement (Proof.context_of state) Thm.theoremK)
wenzelm@19385
   512
  |> Pretty.chunks2 |> Pretty.string_of;
wenzelm@5880
   513
wenzelm@19385
   514
fun string_of_thms state args =
wenzelm@12055
   515
  Pretty.string_of (ProofContext.pretty_thms (Proof.context_of state)
wenzelm@19385
   516
    (Proof.get_thmss state args));
wenzelm@5895
   517
wenzelm@19385
   518
fun string_of_prfs full state arg =
wenzelm@17066
   519
  Pretty.string_of (case arg of
skalberg@15531
   520
      NONE =>
berghofe@12125
   521
        let
wenzelm@17066
   522
          val (ctxt, (_, thm)) = Proof.get_goal state;
wenzelm@26626
   523
          val thy = ProofContext.theory_of ctxt;
wenzelm@26626
   524
          val prf = Thm.proof_of thm;
wenzelm@17066
   525
          val prop = Thm.full_prop_of thm;
berghofe@12125
   526
          val prf' = Proofterm.rewrite_proof_notypes ([], []) prf
berghofe@12125
   527
        in
wenzelm@27258
   528
          ProofSyntax.pretty_proof ctxt
wenzelm@17066
   529
            (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
berghofe@12125
   530
        end
skalberg@15531
   531
    | SOME args => Pretty.chunks
wenzelm@27258
   532
        (map (ProofSyntax.pretty_proof_of (Proof.context_of state) full)
wenzelm@19385
   533
          (Proof.get_thmss state args)));
berghofe@11524
   534
wenzelm@19385
   535
fun string_of_prop state s =
wenzelm@5831
   536
  let
wenzelm@12055
   537
    val ctxt = Proof.context_of state;
wenzelm@24508
   538
    val prop = Syntax.read_prop ctxt s;
wenzelm@26704
   539
    val ctxt' = Variable.auto_fixes prop ctxt;
wenzelm@26704
   540
  in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
wenzelm@5831
   541
wenzelm@19385
   542
fun string_of_term state s =
wenzelm@5831
   543
  let
wenzelm@12055
   544
    val ctxt = Proof.context_of state;
wenzelm@24508
   545
    val t = Syntax.read_term ctxt s;
wenzelm@5831
   546
    val T = Term.type_of t;
wenzelm@26704
   547
    val ctxt' = Variable.auto_fixes t ctxt;
wenzelm@5831
   548
  in
wenzelm@19385
   549
    Pretty.string_of
wenzelm@26704
   550
      (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
wenzelm@26704
   551
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
wenzelm@9128
   552
  end;
wenzelm@5831
   553
wenzelm@19385
   554
fun string_of_type state s =
wenzelm@5831
   555
  let
wenzelm@12055
   556
    val ctxt = Proof.context_of state;
wenzelm@25331
   557
    val T = Syntax.read_typ ctxt s;
wenzelm@24920
   558
  in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end;
wenzelm@9128
   559
wenzelm@23935
   560
fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
wenzelm@23935
   561
  PrintMode.with_modes modes (fn () =>
wenzelm@23935
   562
    writeln (string_of (Toplevel.enter_proof_body state) arg)) ());
wenzelm@19385
   563
wenzelm@19385
   564
in
wenzelm@9128
   565
wenzelm@19268
   566
val print_stmts = print_item string_of_stmts;
wenzelm@12876
   567
val print_thms = print_item string_of_thms;
wenzelm@19385
   568
val print_prfs = print_item o string_of_prfs;
wenzelm@12876
   569
val print_prop = print_item string_of_prop;
wenzelm@12876
   570
val print_term = print_item string_of_term;
wenzelm@12876
   571
val print_type = print_item string_of_type;
wenzelm@5831
   572
wenzelm@19385
   573
end;
wenzelm@19385
   574
wenzelm@12938
   575
wenzelm@12938
   576
(* markup commands *)
wenzelm@12938
   577
wenzelm@27356
   578
fun check_text s state =
wenzelm@27356
   579
  (ThyOutput.eval_antiquote (#1 (OuterKeyword.get_lexicons ())) state s; ());
wenzelm@27356
   580
wenzelm@12938
   581
fun add_header s = Toplevel.keep' (fn int => fn state =>
wenzelm@21858
   582
  (if Toplevel.is_toplevel state then () else raise Toplevel.UNDEF;
wenzelm@27356
   583
   if int then check_text s NONE else ()));
wenzelm@12938
   584
wenzelm@12953
   585
local
wenzelm@12953
   586
wenzelm@27356
   587
fun present _ txt true node = check_text txt (SOME node)
wenzelm@26415
   588
  | present f (s, _) false node = Context.setmp_thread_data
wenzelm@26415
   589
      (try (Toplevel.cases_node I (Context.Proof o Proof.context_of)) node) f s;
wenzelm@12953
   590
wenzelm@19057
   591
fun present_local_theory f (loc, txt) = Toplevel.present_local_theory loc (present f txt);
wenzelm@19057
   592
fun present_proof f txt = Toplevel.print o Toplevel.present_proof (present f txt);
wenzelm@12938
   593
wenzelm@12953
   594
in
wenzelm@12953
   595
wenzelm@19057
   596
val add_chapter       = present_local_theory Present.section;
wenzelm@19057
   597
val add_section       = present_local_theory Present.section;
wenzelm@19057
   598
val add_subsection    = present_local_theory Present.subsection;
wenzelm@19057
   599
val add_subsubsection = present_local_theory Present.subsubsection;
wenzelm@19057
   600
val add_text          = present_local_theory (K ());
wenzelm@19057
   601
fun add_text_raw txt  = present_local_theory (K ()) (NONE, txt);
wenzelm@19057
   602
val add_txt           = present_proof (K ());
wenzelm@19057
   603
val add_txt_raw       = add_txt;
wenzelm@19057
   604
val add_sect          = add_txt;
wenzelm@19057
   605
val add_subsect       = add_txt;
wenzelm@19057
   606
val add_subsubsect    = add_txt;
wenzelm@12938
   607
wenzelm@5831
   608
end;
wenzelm@12953
   609
wenzelm@12953
   610
end;