src/Pure/Isar/isar_cmd.ML
author wenzelm
Fri Jul 25 15:01:18 2014 +0200 (2014-07-25 ago)
changeset 57683 cc0aa6528890
parent 57605 8e0a7eaffe47
child 57934 5e500c0e7eca
permissions -rw-r--r--
old 'defs' is legacy --- slightly odd side-entry that bypasses regular Local_Theory.define interface;
wenzelm@5831
     1
(*  Title:      Pure/Isar/isar_cmd.ML
wenzelm@5831
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5831
     3
wenzelm@30805
     4
Miscellaneous Isar commands.
wenzelm@5831
     5
*)
wenzelm@5831
     6
wenzelm@5831
     7
signature ISAR_CMD =
wenzelm@5831
     8
sig
wenzelm@55828
     9
  val global_setup: Symbol_Pos.source -> theory -> theory
wenzelm@55828
    10
  val local_setup: Symbol_Pos.source -> Proof.context -> Proof.context
wenzelm@55828
    11
  val parse_ast_translation: Symbol_Pos.source -> theory -> theory
wenzelm@55828
    12
  val parse_translation: Symbol_Pos.source -> theory -> theory
wenzelm@55828
    13
  val print_translation: Symbol_Pos.source -> theory -> theory
wenzelm@55828
    14
  val typed_print_translation: Symbol_Pos.source -> theory -> theory
wenzelm@55828
    15
  val print_ast_translation: Symbol_Pos.source -> theory -> theory
wenzelm@42204
    16
  val translations: (xstring * string) Syntax.trrule list -> theory -> theory
wenzelm@42204
    17
  val no_translations: (xstring * string) Syntax.trrule list -> theory -> theory
wenzelm@55828
    18
  val oracle: bstring * Position.T -> Symbol_Pos.source -> theory -> theory
haftmann@29579
    19
  val add_defs: (bool * bool) * ((binding * string) * Attrib.src list) list -> theory -> theory
wenzelm@40784
    20
  val declaration: {syntax: bool, pervasive: bool} ->
wenzelm@55828
    21
    Symbol_Pos.source -> local_theory -> local_theory
wenzelm@55828
    22
  val simproc_setup: string * Position.T -> string list -> Symbol_Pos.source ->
wenzelm@42464
    23
    string list -> local_theory -> local_theory
wenzelm@28084
    24
  val have: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
wenzelm@28084
    25
  val hence: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
wenzelm@28084
    26
  val show: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
wenzelm@28084
    27
  val thus: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
wenzelm@49889
    28
  val qed: Method.text_range option -> Toplevel.transition -> Toplevel.transition
wenzelm@49889
    29
  val terminal_proof: Method.text_range * Method.text_range option ->
wenzelm@21350
    30
    Toplevel.transition -> Toplevel.transition
wenzelm@21350
    31
  val default_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    32
  val immediate_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    33
  val done_proof: Toplevel.transition -> Toplevel.transition
wenzelm@21350
    34
  val skip_proof: Toplevel.transition -> Toplevel.transition
wenzelm@55828
    35
  val ml_diag: bool -> Symbol_Pos.source -> Toplevel.transition -> Toplevel.transition
wenzelm@48776
    36
  val diag_state: Proof.context -> Toplevel.state
wenzelm@48776
    37
  val diag_goal: Proof.context -> {context: Proof.context, facts: thm list, goal: thm}
wenzelm@57605
    38
  val pretty_theorems: bool -> Toplevel.state -> Pretty.T list
wenzelm@49569
    39
  val thy_deps: Toplevel.transition -> Toplevel.transition
wenzelm@49569
    40
  val locale_deps: Toplevel.transition -> Toplevel.transition
wenzelm@20574
    41
  val class_deps: Toplevel.transition -> Toplevel.transition
wenzelm@26336
    42
  val thm_deps: (Facts.ref * Attrib.src list) list -> Toplevel.transition -> Toplevel.transition
berghofe@26184
    43
  val unused_thms: (string list * string list option) option ->
berghofe@26184
    44
    Toplevel.transition -> Toplevel.transition
wenzelm@26336
    45
  val print_stmts: string list * (Facts.ref * Attrib.src list) list
wenzelm@19268
    46
    -> Toplevel.transition -> Toplevel.transition
wenzelm@26336
    47
  val print_thms: string list * (Facts.ref * Attrib.src list) list
wenzelm@10581
    48
    -> Toplevel.transition -> Toplevel.transition
wenzelm@26336
    49
  val print_prfs: bool -> string list * (Facts.ref * Attrib.src list) list option
berghofe@11524
    50
    -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    51
  val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
    52
  val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
wenzelm@48792
    53
  val print_type: (string list * (string * string option)) ->
wenzelm@48792
    54
    Toplevel.transition -> Toplevel.transition
wenzelm@55828
    55
  val header_markup: Symbol_Pos.source -> Toplevel.transition -> Toplevel.transition
wenzelm@55828
    56
  val local_theory_markup: (xstring * Position.T) option * (Symbol_Pos.source) ->
wenzelm@17262
    57
    Toplevel.transition -> Toplevel.transition
wenzelm@55828
    58
  val proof_markup: Symbol_Pos.source -> Toplevel.transition -> Toplevel.transition
wenzelm@5831
    59
end;
wenzelm@5831
    60
wenzelm@37216
    61
structure Isar_Cmd: ISAR_CMD =
wenzelm@5831
    62
struct
wenzelm@5831
    63
wenzelm@5831
    64
wenzelm@22116
    65
(** theory declarations **)
wenzelm@22116
    66
wenzelm@30461
    67
(* generic setup *)
wenzelm@22116
    68
wenzelm@55828
    69
fun global_setup source =
wenzelm@56278
    70
  ML_Lex.read_source false source
wenzelm@55828
    71
  |> ML_Context.expression (#pos source) "val setup: theory -> theory" "Context.map_theory setup"
wenzelm@26435
    72
  |> Context.theory_map;
wenzelm@22116
    73
wenzelm@55828
    74
fun local_setup source =
wenzelm@56278
    75
  ML_Lex.read_source false source
wenzelm@55828
    76
  |> ML_Context.expression (#pos source) "val setup: local_theory -> local_theory" "Context.map_proof setup"
wenzelm@30461
    77
  |> Context.proof_map;
wenzelm@30461
    78
wenzelm@22116
    79
wenzelm@22116
    80
(* translation functions *)
wenzelm@22116
    81
wenzelm@55828
    82
fun parse_ast_translation source =
wenzelm@56278
    83
  ML_Lex.read_source false source
wenzelm@55828
    84
  |> ML_Context.expression (#pos source)
wenzelm@52143
    85
    "val parse_ast_translation: (string * (Proof.context -> Ast.ast list -> Ast.ast)) list"
wenzelm@52143
    86
    "Context.map_theory (Sign.parse_ast_translation parse_ast_translation)"
wenzelm@22116
    87
  |> Context.theory_map;
wenzelm@22116
    88
wenzelm@55828
    89
fun parse_translation source =
wenzelm@56278
    90
  ML_Lex.read_source false source
wenzelm@55828
    91
  |> ML_Context.expression (#pos source)
wenzelm@52143
    92
    "val parse_translation: (string * (Proof.context -> term list -> term)) list"
wenzelm@52143
    93
    "Context.map_theory (Sign.parse_translation parse_translation)"
wenzelm@52143
    94
  |> Context.theory_map;
wenzelm@52143
    95
wenzelm@55828
    96
fun print_translation source =
wenzelm@56278
    97
  ML_Lex.read_source false source
wenzelm@55828
    98
  |> ML_Context.expression (#pos source)
wenzelm@52143
    99
    "val print_translation: (string * (Proof.context -> term list -> term)) list"
wenzelm@52143
   100
    "Context.map_theory (Sign.print_translation print_translation)"
wenzelm@22116
   101
  |> Context.theory_map;
wenzelm@22116
   102
wenzelm@55828
   103
fun typed_print_translation source =
wenzelm@56278
   104
  ML_Lex.read_source false source
wenzelm@55828
   105
  |> ML_Context.expression (#pos source)
wenzelm@52143
   106
    "val typed_print_translation: (string * (Proof.context -> typ -> term list -> term)) list"
wenzelm@52143
   107
    "Context.map_theory (Sign.typed_print_translation typed_print_translation)"
wenzelm@22116
   108
  |> Context.theory_map;
wenzelm@22116
   109
wenzelm@55828
   110
fun print_ast_translation source =
wenzelm@56278
   111
  ML_Lex.read_source false source
wenzelm@55828
   112
  |> ML_Context.expression (#pos source)
wenzelm@52143
   113
    "val print_ast_translation: (string * (Proof.context -> Ast.ast list -> Ast.ast)) list"
wenzelm@52143
   114
    "Context.map_theory (Sign.print_ast_translation print_ast_translation)"
wenzelm@22116
   115
  |> Context.theory_map;
wenzelm@22116
   116
wenzelm@22116
   117
wenzelm@42204
   118
(* translation rules *)
wenzelm@42204
   119
wenzelm@42204
   120
fun read_trrules thy raw_rules =
wenzelm@42204
   121
  let
wenzelm@42360
   122
    val ctxt = Proof_Context.init_global thy;
wenzelm@56006
   123
    val read_root =
wenzelm@56006
   124
      #1 o dest_Type o Proof_Context.read_type_name {proper = true, strict = false} ctxt;
wenzelm@42204
   125
  in
wenzelm@56006
   126
    raw_rules
wenzelm@56006
   127
    |> map (Syntax.map_trrule (fn (r, s) => Syntax_Phases.parse_ast_pattern ctxt (read_root r, s)))
wenzelm@42204
   128
  end;
wenzelm@42204
   129
wenzelm@42204
   130
fun translations args thy = Sign.add_trrules (read_trrules thy args) thy;
wenzelm@42204
   131
fun no_translations args thy = Sign.del_trrules (read_trrules thy args) thy;
wenzelm@42204
   132
wenzelm@42204
   133
wenzelm@22116
   134
(* oracles *)
wenzelm@22116
   135
wenzelm@55828
   136
fun oracle (name, pos) source =
wenzelm@27871
   137
  let
wenzelm@56278
   138
    val body = ML_Lex.read_source false source;
wenzelm@37198
   139
    val ants =
wenzelm@37198
   140
      ML_Lex.read Position.none
wenzelm@37198
   141
       ("local\n\
wenzelm@37198
   142
        \  val binding = " ^ ML_Syntax.make_binding (name, pos) ^ ";\n\
wenzelm@37198
   143
        \  val body = ") @ body @ ML_Lex.read Position.none (";\n\
wenzelm@37198
   144
        \in\n\
wenzelm@55828
   145
        \  val " ^ name ^
wenzelm@55828
   146
        " = snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (binding, body))));\n\
wenzelm@37198
   147
        \end;\n");
wenzelm@56304
   148
  in
wenzelm@56304
   149
    Context.theory_map
wenzelm@56304
   150
      (ML_Context.exec (fn () => ML_Context.eval ML_Compiler.flags (#pos source) ants))
wenzelm@56304
   151
  end;
wenzelm@22116
   152
wenzelm@22116
   153
wenzelm@50737
   154
(* old-style defs *)
wenzelm@21350
   155
wenzelm@35852
   156
fun add_defs ((unchecked, overloaded), args) thy =
wenzelm@57683
   157
 (legacy_feature "Old 'defs' command -- use 'definition' (with 'overloading') instead";
wenzelm@39557
   158
  thy |>
wenzelm@39557
   159
    (if unchecked then Global_Theory.add_defs_unchecked_cmd else Global_Theory.add_defs_cmd)
wenzelm@47815
   160
      overloaded
wenzelm@47815
   161
      (map (fn ((b, ax), srcs) => ((b, ax), map (Attrib.attribute_cmd_global thy) srcs)) args)
wenzelm@57683
   162
  |> snd);
wenzelm@21350
   163
wenzelm@21350
   164
wenzelm@22087
   165
(* declarations *)
wenzelm@22087
   166
wenzelm@55828
   167
fun declaration {syntax, pervasive} source =
wenzelm@56278
   168
  ML_Lex.read_source false source
wenzelm@55828
   169
  |> ML_Context.expression (#pos source)
wenzelm@26455
   170
    "val declaration: Morphism.declaration"
wenzelm@45291
   171
    ("Context.map_proof (Local_Theory.declaration {syntax = " ^ Bool.toString syntax ^ ", \
wenzelm@45291
   172
      \pervasive = " ^ Bool.toString pervasive ^ "} declaration)")
wenzelm@26385
   173
  |> Context.proof_map;
wenzelm@22087
   174
wenzelm@22087
   175
wenzelm@22202
   176
(* simprocs *)
wenzelm@22202
   177
wenzelm@55828
   178
fun simproc_setup name lhss source identifier =
wenzelm@56278
   179
  ML_Lex.read_source false source
wenzelm@55828
   180
  |> ML_Context.expression (#pos source)
wenzelm@51717
   181
    "val proc: Morphism.morphism -> Proof.context -> cterm -> thm option"
wenzelm@42464
   182
    ("Context.map_proof (Simplifier.def_simproc_cmd {name = " ^ ML_Syntax.make_binding name ^ ", \
wenzelm@37198
   183
      \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
wenzelm@37198
   184
      \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})")
wenzelm@22202
   185
  |> Context.proof_map;
wenzelm@22202
   186
wenzelm@22202
   187
wenzelm@21350
   188
(* goals *)
wenzelm@21350
   189
wenzelm@21350
   190
fun goal opt_chain goal stmt int =
wenzelm@29383
   191
  opt_chain #> goal NONE (K I) stmt int;
wenzelm@21350
   192
wenzelm@36323
   193
val have = goal I Proof.have_cmd;
wenzelm@36323
   194
val hence = goal Proof.chain Proof.have_cmd;
wenzelm@36323
   195
val show = goal I Proof.show_cmd;
wenzelm@36323
   196
val thus = goal Proof.chain Proof.show_cmd;
wenzelm@21350
   197
wenzelm@21350
   198
wenzelm@21350
   199
(* local endings *)
wenzelm@21350
   200
wenzelm@29383
   201
fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
wenzelm@32061
   202
val local_terminal_proof = Toplevel.proof' o Proof.local_future_terminal_proof;
wenzelm@29383
   203
val local_default_proof = Toplevel.proof Proof.local_default_proof;
wenzelm@29383
   204
val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
wenzelm@29383
   205
val local_done_proof = Toplevel.proof Proof.local_done_proof;
wenzelm@29383
   206
val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
wenzelm@21350
   207
wenzelm@27562
   208
val skip_local_qed = Toplevel.skip_proof (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF);
wenzelm@21350
   209
wenzelm@21350
   210
wenzelm@21350
   211
(* global endings *)
wenzelm@21350
   212
wenzelm@21350
   213
fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
wenzelm@49012
   214
val global_terminal_proof = Toplevel.end_proof o Proof.global_future_terminal_proof;
wenzelm@21350
   215
val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
wenzelm@21350
   216
val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
wenzelm@21350
   217
val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
wenzelm@21350
   218
val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
wenzelm@21350
   219
wenzelm@28375
   220
val skip_global_qed = Toplevel.skip_proof_to_theory (fn n => n = 1);
wenzelm@21350
   221
wenzelm@21350
   222
wenzelm@21350
   223
(* common endings *)
wenzelm@21350
   224
wenzelm@21350
   225
fun qed m = local_qed m o global_qed m o skip_local_qed o skip_global_qed;
wenzelm@21350
   226
fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
wenzelm@21350
   227
val default_proof = local_default_proof o global_default_proof;
wenzelm@21350
   228
val immediate_proof = local_immediate_proof o global_immediate_proof;
wenzelm@21350
   229
val done_proof = local_done_proof o global_done_proof;
wenzelm@21350
   230
val skip_proof = local_skip_proof o global_skip_proof;
wenzelm@21350
   231
wenzelm@21350
   232
wenzelm@26489
   233
(* diagnostic ML evaluation *)
wenzelm@5831
   234
wenzelm@37305
   235
structure Diag_State = Proof_Data
wenzelm@37305
   236
(
wenzelm@37305
   237
  type T = Toplevel.state;
wenzelm@37305
   238
  fun init _ = Toplevel.toplevel;
wenzelm@37305
   239
);
wenzelm@37305
   240
wenzelm@55828
   241
fun ml_diag verbose source = Toplevel.keep (fn state =>
wenzelm@56304
   242
  let
wenzelm@56304
   243
    val opt_ctxt =
wenzelm@56304
   244
      try Toplevel.generic_theory_of state
wenzelm@56304
   245
      |> Option.map (Context.proof_of #> Diag_State.put state);
wenzelm@56304
   246
    val flags = ML_Compiler.verbose verbose ML_Compiler.flags;
wenzelm@56304
   247
  in ML_Context.eval_source_in opt_ctxt flags source end);
wenzelm@37305
   248
wenzelm@48776
   249
val diag_state = Diag_State.get;
wenzelm@37305
   250
wenzelm@48776
   251
fun diag_goal ctxt =
wenzelm@48776
   252
  Proof.goal (Toplevel.proof_of (diag_state ctxt))
wenzelm@37305
   253
    handle Toplevel.UNDEF => error "No goal present";
wenzelm@37305
   254
wenzelm@53171
   255
val _ = Theory.setup
wenzelm@56204
   256
  (ML_Antiquotation.value (Binding.qualify true "Isar" @{binding state})
wenzelm@53171
   257
    (Scan.succeed "Isar_Cmd.diag_state ML_context") #>
wenzelm@56204
   258
   ML_Antiquotation.value (Binding.qualify true "Isar" @{binding goal})
wenzelm@53171
   259
    (Scan.succeed "Isar_Cmd.diag_goal ML_context"));
wenzelm@5831
   260
wenzelm@5831
   261
wenzelm@56868
   262
(* theorems of theory or proof context *)
wenzelm@17066
   263
wenzelm@56868
   264
fun pretty_theorems verbose st =
wenzelm@56868
   265
  if Toplevel.is_proof st then
wenzelm@57605
   266
    Proof_Context.pretty_local_facts (Toplevel.context_of st) verbose
wenzelm@56868
   267
  else
wenzelm@56868
   268
    let
wenzelm@56868
   269
      val thy = Toplevel.theory_of st;
wenzelm@56868
   270
      val prev_thys =
wenzelm@56868
   271
        (case Toplevel.previous_context_of st of
wenzelm@56868
   272
          SOME prev => [Proof_Context.theory_of prev]
wenzelm@56868
   273
        | NONE => Theory.parents_of thy);
wenzelm@56868
   274
    in Proof_Display.pretty_theorems_diff verbose prev_thys thy end;
wenzelm@9513
   275
wenzelm@12060
   276
wenzelm@50737
   277
(* display dependencies *)
wenzelm@5831
   278
haftmann@22485
   279
val thy_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
haftmann@22485
   280
  let
haftmann@22485
   281
    val thy = Toplevel.theory_of state;
wenzelm@37866
   282
    val thy_session = Present.session_name thy;
wenzelm@37866
   283
wenzelm@42425
   284
    val gr = rev (Theory.nodes_of thy) |> map (fn node =>
wenzelm@22602
   285
      let
wenzelm@22602
   286
        val name = Context.theory_name node;
wenzelm@22602
   287
        val parents = map Context.theory_name (Theory.parents_of node);
wenzelm@37866
   288
        val session = Present.session_name node;
wenzelm@37866
   289
        val unfold = (session = thy_session);
wenzelm@49561
   290
      in
wenzelm@49561
   291
       {name = name, ID = name, parents = parents, dir = session,
wenzelm@49561
   292
        unfold = unfold, path = "", content = []}
wenzelm@49561
   293
      end);
wenzelm@49561
   294
  in Graph_Display.display_graph gr end);
haftmann@22485
   295
wenzelm@49569
   296
val locale_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@49569
   297
  let
wenzelm@49569
   298
    val thy = Toplevel.theory_of state;
wenzelm@49569
   299
    val gr = Locale.pretty_locale_deps thy |> map (fn {name, parents, body} =>
wenzelm@49569
   300
     {name = Locale.extern thy name, ID = name, parents = parents,
wenzelm@49569
   301
      dir = "", unfold = true, path = "", content = [body]});
wenzelm@49569
   302
  in Graph_Display.display_graph gr end);
wenzelm@49569
   303
wenzelm@20574
   304
val class_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@20574
   305
  let
wenzelm@42358
   306
    val ctxt = Toplevel.context_of state;
wenzelm@42360
   307
    val {classes = (space, algebra), ...} = Type.rep_tsig (Proof_Context.tsig_of ctxt);
wenzelm@36328
   308
    val classes = Sorts.classes_of algebra;
wenzelm@20574
   309
    fun entry (c, (i, (_, cs))) =
wenzelm@44338
   310
      (i, {name = Name_Space.extern ctxt space c, ID = c, parents = Graph.Keys.dest cs,
wenzelm@49561
   311
            dir = "", unfold = true, path = "", content = []});
wenzelm@20574
   312
    val gr =
wenzelm@20574
   313
      Graph.fold (cons o entry) classes []
wenzelm@20574
   314
      |> sort (int_ord o pairself #1) |> map #2;
wenzelm@49561
   315
  in Graph_Display.display_graph gr end);
wenzelm@20574
   316
wenzelm@9513
   317
fun thm_deps args = Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@37870
   318
  Thm_Deps.thm_deps (Toplevel.theory_of state)
wenzelm@38331
   319
    (Attrib.eval_thms (Toplevel.context_of state) args));
wenzelm@9454
   320
wenzelm@5831
   321
berghofe@26184
   322
(* find unused theorems *)
berghofe@26184
   323
wenzelm@26186
   324
fun unused_thms opt_range = Toplevel.keep (fn state =>
wenzelm@26186
   325
  let
wenzelm@26186
   326
    val thy = Toplevel.theory_of state;
wenzelm@26186
   327
    val ctxt = Toplevel.context_of state;
wenzelm@42360
   328
    fun pretty_thm (a, th) = Proof_Context.pretty_fact ctxt (a, [th]);
wenzelm@37870
   329
    val get_theory = Context.get_theory thy;
wenzelm@26186
   330
  in
wenzelm@33391
   331
    Thm_Deps.unused_thms
wenzelm@26186
   332
      (case opt_range of
wenzelm@26694
   333
        NONE => (Theory.parents_of thy, [thy])
wenzelm@37870
   334
      | SOME (xs, NONE) => (map get_theory xs, [thy])
wenzelm@37870
   335
      | SOME (xs, SOME ys) => (map get_theory xs, map get_theory ys))
wenzelm@56334
   336
    |> map pretty_thm |> Pretty.writeln_chunks
wenzelm@26186
   337
  end);
berghofe@26184
   338
berghofe@26184
   339
wenzelm@19268
   340
(* print theorems, terms, types etc. *)
wenzelm@19268
   341
wenzelm@19385
   342
local
wenzelm@19385
   343
wenzelm@38331
   344
fun string_of_stmts ctxt args =
wenzelm@38331
   345
  Attrib.eval_thms ctxt args
wenzelm@38331
   346
  |> map (Element.pretty_statement ctxt Thm.theoremK)
wenzelm@19385
   347
  |> Pretty.chunks2 |> Pretty.string_of;
wenzelm@5880
   348
wenzelm@38331
   349
fun string_of_thms ctxt args =
wenzelm@51583
   350
  Pretty.string_of (Proof_Context.pretty_fact ctxt ("", Attrib.eval_thms ctxt args));
wenzelm@5895
   351
wenzelm@19385
   352
fun string_of_prfs full state arg =
wenzelm@32859
   353
  Pretty.string_of
wenzelm@32859
   354
    (case arg of
skalberg@15531
   355
      NONE =>
berghofe@12125
   356
        let
wenzelm@38331
   357
          val {context = ctxt, goal = thm} = Proof.simple_goal (Toplevel.proof_of state);
wenzelm@42360
   358
          val thy = Proof_Context.theory_of ctxt;
wenzelm@28814
   359
          val prf = Thm.proof_of thm;
wenzelm@17066
   360
          val prop = Thm.full_prop_of thm;
wenzelm@32859
   361
          val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
berghofe@12125
   362
        in
wenzelm@33388
   363
          Proof_Syntax.pretty_proof ctxt
wenzelm@17066
   364
            (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
berghofe@12125
   365
        end
wenzelm@38331
   366
    | SOME srcs =>
wenzelm@38331
   367
        let val ctxt = Toplevel.context_of state
wenzelm@38331
   368
        in map (Proof_Syntax.pretty_proof_of ctxt full) (Attrib.eval_thms ctxt srcs) end
wenzelm@38331
   369
        |> Pretty.chunks);
berghofe@11524
   370
wenzelm@38331
   371
fun string_of_prop ctxt s =
wenzelm@5831
   372
  let
wenzelm@24508
   373
    val prop = Syntax.read_prop ctxt s;
wenzelm@26704
   374
    val ctxt' = Variable.auto_fixes prop ctxt;
wenzelm@26704
   375
  in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
wenzelm@5831
   376
wenzelm@38331
   377
fun string_of_term ctxt s =
wenzelm@5831
   378
  let
wenzelm@24508
   379
    val t = Syntax.read_term ctxt s;
wenzelm@5831
   380
    val T = Term.type_of t;
wenzelm@26704
   381
    val ctxt' = Variable.auto_fixes t ctxt;
wenzelm@5831
   382
  in
wenzelm@19385
   383
    Pretty.string_of
wenzelm@26704
   384
      (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
wenzelm@26704
   385
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
wenzelm@9128
   386
  end;
wenzelm@5831
   387
wenzelm@48792
   388
fun string_of_type ctxt (s, NONE) =
wenzelm@48792
   389
      let val T = Syntax.read_typ ctxt s
wenzelm@48792
   390
      in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end
wenzelm@48792
   391
  | string_of_type ctxt (s1, SOME s2) =
wenzelm@48792
   392
      let
wenzelm@48792
   393
        val ctxt' = Config.put show_sorts true ctxt;
wenzelm@48792
   394
        val raw_T = Syntax.parse_typ ctxt' s1;
wenzelm@48792
   395
        val S = Syntax.read_sort ctxt' s2;
wenzelm@48792
   396
        val T =
wenzelm@48792
   397
          Syntax.check_term ctxt'
wenzelm@48792
   398
            (Logic.mk_type raw_T |> Type.constraint (Term.itselfT (Type_Infer.anyT S)))
wenzelm@48792
   399
          |> Logic.dest_type;
wenzelm@48792
   400
      in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt' T)) end;
wenzelm@9128
   401
wenzelm@23935
   402
fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
wenzelm@38331
   403
  Print_Mode.with_modes modes (fn () => writeln (string_of state arg)) ());
wenzelm@19385
   404
wenzelm@19385
   405
in
wenzelm@9128
   406
wenzelm@38331
   407
val print_stmts = print_item (string_of_stmts o Toplevel.context_of);
wenzelm@38331
   408
val print_thms = print_item (string_of_thms o Toplevel.context_of);
wenzelm@19385
   409
val print_prfs = print_item o string_of_prfs;
wenzelm@38331
   410
val print_prop = print_item (string_of_prop o Toplevel.context_of);
wenzelm@38331
   411
val print_term = print_item (string_of_term o Toplevel.context_of);
wenzelm@38331
   412
val print_type = print_item (string_of_type o Toplevel.context_of);
wenzelm@5831
   413
wenzelm@19385
   414
end;
wenzelm@19385
   415
wenzelm@12938
   416
wenzelm@12938
   417
(* markup commands *)
wenzelm@12938
   418
wenzelm@27853
   419
fun header_markup txt = Toplevel.keep (fn state =>
wenzelm@48918
   420
  if Toplevel.is_toplevel state then Thy_Output.check_text txt state
wenzelm@27853
   421
  else raise Toplevel.UNDEF);
wenzelm@12953
   422
wenzelm@48918
   423
fun local_theory_markup (loc, txt) = Toplevel.present_local_theory loc (Thy_Output.check_text txt);
wenzelm@48918
   424
val proof_markup = Toplevel.present_proof o Thy_Output.check_text;
wenzelm@12938
   425
wenzelm@5831
   426
end;