src/Pure/Isar/isar_cmd.ML
author wenzelm
Fri Nov 13 21:11:15 2009 +0100 (2009-11-13 ago)
changeset 33671 4b0f2599ed48
parent 33515 d066e8369a33
child 35141 182f27a8716c
permissions -rw-r--r--
modernized structure Local_Theory;
     1 (*  Title:      Pure/Isar/isar_cmd.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Miscellaneous Isar commands.
     5 *)
     6 
     7 signature ISAR_CMD =
     8 sig
     9   val global_setup: Symbol_Pos.text * Position.T -> theory -> theory
    10   val local_setup: Symbol_Pos.text * Position.T -> Proof.context -> Proof.context
    11   val parse_ast_translation: bool * (Symbol_Pos.text * Position.T) -> theory -> theory
    12   val parse_translation: bool * (Symbol_Pos.text * Position.T) -> theory -> theory
    13   val print_translation: bool * (Symbol_Pos.text * Position.T) -> theory -> theory
    14   val typed_print_translation: bool * (Symbol_Pos.text * Position.T) -> theory -> theory
    15   val print_ast_translation: bool * (Symbol_Pos.text * Position.T) -> theory -> theory
    16   val oracle: bstring * Position.T -> Symbol_Pos.text * Position.T -> theory -> theory
    17   val add_axioms: ((binding * string) * Attrib.src list) list -> theory -> theory
    18   val add_defs: (bool * bool) * ((binding * string) * Attrib.src list) list -> theory -> theory
    19   val declaration: bool -> Symbol_Pos.text * Position.T -> local_theory -> local_theory
    20   val simproc_setup: string -> string list -> Symbol_Pos.text * Position.T -> string list ->
    21     local_theory -> local_theory
    22   val hide_names: bool -> string * xstring list -> theory -> theory
    23   val have: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    24   val hence: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    25   val show: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    26   val thus: (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
    27   val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
    28   val terminal_proof: Method.text * Method.text option ->
    29     Toplevel.transition -> Toplevel.transition
    30   val default_proof: Toplevel.transition -> Toplevel.transition
    31   val immediate_proof: Toplevel.transition -> Toplevel.transition
    32   val done_proof: Toplevel.transition -> Toplevel.transition
    33   val skip_proof: Toplevel.transition -> Toplevel.transition
    34   val init_theory: string * string list * (string * bool) list ->
    35     Toplevel.transition -> Toplevel.transition
    36   val exit: Toplevel.transition -> Toplevel.transition
    37   val quit: Toplevel.transition -> Toplevel.transition
    38   val pr: string list * (int option * int option) -> Toplevel.transition -> Toplevel.transition
    39   val disable_pr: Toplevel.transition -> Toplevel.transition
    40   val enable_pr: Toplevel.transition -> Toplevel.transition
    41   val ml_diag: bool -> Symbol_Pos.text * Position.T -> Toplevel.transition -> Toplevel.transition
    42   val cd: Path.T -> Toplevel.transition -> Toplevel.transition
    43   val pwd: Toplevel.transition -> Toplevel.transition
    44   val display_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
    45   val print_drafts: Path.T list -> Toplevel.transition -> Toplevel.transition
    46   val pretty_setmargin: int -> Toplevel.transition -> Toplevel.transition
    47   val print_context: Toplevel.transition -> Toplevel.transition
    48   val print_theory: bool -> Toplevel.transition -> Toplevel.transition
    49   val print_syntax: Toplevel.transition -> Toplevel.transition
    50   val print_abbrevs: Toplevel.transition -> Toplevel.transition
    51   val print_facts: Toplevel.transition -> Toplevel.transition
    52   val print_configs: Toplevel.transition -> Toplevel.transition
    53   val print_theorems: bool -> Toplevel.transition -> Toplevel.transition
    54   val print_locales: Toplevel.transition -> Toplevel.transition
    55   val print_locale: bool * xstring -> Toplevel.transition -> Toplevel.transition
    56   val print_registrations: string -> Toplevel.transition -> Toplevel.transition
    57   val print_attributes: Toplevel.transition -> Toplevel.transition
    58   val print_simpset: Toplevel.transition -> Toplevel.transition
    59   val print_rules: Toplevel.transition -> Toplevel.transition
    60   val print_trans_rules: Toplevel.transition -> Toplevel.transition
    61   val print_methods: Toplevel.transition -> Toplevel.transition
    62   val print_antiquotations: Toplevel.transition -> Toplevel.transition
    63   val class_deps: Toplevel.transition -> Toplevel.transition
    64   val thy_deps: Toplevel.transition -> Toplevel.transition
    65   val thm_deps: (Facts.ref * Attrib.src list) list -> Toplevel.transition -> Toplevel.transition
    66   val unused_thms: (string list * string list option) option ->
    67     Toplevel.transition -> Toplevel.transition
    68   val print_binds: Toplevel.transition -> Toplevel.transition
    69   val print_cases: Toplevel.transition -> Toplevel.transition
    70   val print_stmts: string list * (Facts.ref * Attrib.src list) list
    71     -> Toplevel.transition -> Toplevel.transition
    72   val print_thms: string list * (Facts.ref * Attrib.src list) list
    73     -> Toplevel.transition -> Toplevel.transition
    74   val print_prfs: bool -> string list * (Facts.ref * Attrib.src list) list option
    75     -> Toplevel.transition -> Toplevel.transition
    76   val print_prop: (string list * string) -> Toplevel.transition -> Toplevel.transition
    77   val print_term: (string list * string) -> Toplevel.transition -> Toplevel.transition
    78   val print_type: (string list * string) -> Toplevel.transition -> Toplevel.transition
    79   val header_markup: Symbol_Pos.text * Position.T -> Toplevel.transition -> Toplevel.transition
    80   val local_theory_markup: xstring option * (Symbol_Pos.text * Position.T) ->
    81     Toplevel.transition -> Toplevel.transition
    82   val proof_markup: Symbol_Pos.text * Position.T -> Toplevel.transition -> Toplevel.transition
    83 end;
    84 
    85 structure IsarCmd: ISAR_CMD =
    86 struct
    87 
    88 
    89 (** theory declarations **)
    90 
    91 (* generic setup *)
    92 
    93 fun global_setup (txt, pos) =
    94   ML_Context.expression pos "val setup: theory -> theory" "Context.map_theory setup" txt
    95   |> Context.theory_map;
    96 
    97 fun local_setup (txt, pos) =
    98   ML_Context.expression pos "val setup: local_theory -> local_theory" "Context.map_proof setup" txt
    99   |> Context.proof_map;
   100 
   101 
   102 (* translation functions *)
   103 
   104 local
   105 
   106 fun advancedT false = ""
   107   | advancedT true = "Proof.context -> ";
   108 
   109 fun advancedN false = ""
   110   | advancedN true = "advanced_";
   111 
   112 in
   113 
   114 fun parse_ast_translation (a, (txt, pos)) =
   115   txt |> ML_Context.expression pos
   116     ("val parse_ast_translation: (string * (" ^ advancedT a ^
   117       "Syntax.ast list -> Syntax.ast)) list")
   118     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns (parse_ast_translation, [], [], []))")
   119   |> Context.theory_map;
   120 
   121 fun parse_translation (a, (txt, pos)) =
   122   txt |> ML_Context.expression pos
   123     ("val parse_translation: (string * (" ^ advancedT a ^
   124       "term list -> term)) list")
   125     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], parse_translation, [], []))")
   126   |> Context.theory_map;
   127 
   128 fun print_translation (a, (txt, pos)) =
   129   txt |> ML_Context.expression pos
   130     ("val print_translation: (string * (" ^ advancedT a ^
   131       "term list -> term)) list")
   132     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], print_translation, []))")
   133   |> Context.theory_map;
   134 
   135 fun print_ast_translation (a, (txt, pos)) =
   136   txt |> ML_Context.expression pos
   137     ("val print_ast_translation: (string * (" ^ advancedT a ^
   138       "Syntax.ast list -> Syntax.ast)) list")
   139     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfuns ([], [], [], print_ast_translation))")
   140   |> Context.theory_map;
   141 
   142 fun typed_print_translation (a, (txt, pos)) =
   143   txt |> ML_Context.expression pos
   144     ("val typed_print_translation: (string * (" ^ advancedT a ^
   145       "bool -> typ -> term list -> term)) list")
   146     ("Context.map_theory (Sign.add_" ^ advancedN a ^ "trfunsT typed_print_translation)")
   147   |> Context.theory_map;
   148 
   149 end;
   150 
   151 
   152 (* oracles *)
   153 
   154 fun oracle (name, pos) (body_txt, body_pos) =
   155   let
   156     val body = Symbol_Pos.content (Symbol_Pos.explode (body_txt, body_pos));
   157     val txt =
   158       "local\n\
   159       \  val binding = " ^ ML_Syntax.make_binding (name, pos) ^ ";\n\
   160       \  val body = " ^ body ^ ";\n\
   161       \in\n\
   162       \  val " ^ name ^ " = snd (Context.>>> (Context.map_theory_result (Thm.add_oracle (binding, body))));\n\
   163       \end;\n";
   164   in Context.theory_map (ML_Context.exec (fn () => ML_Context.eval false body_pos txt)) end;
   165 
   166 
   167 (* axioms *)
   168 
   169 fun add_axms f args thy =
   170   f (map (fn ((b, ax), srcs) => ((b, ax), map (Attrib.attribute thy) srcs)) args) thy;
   171 
   172 val add_axioms = add_axms (snd oo PureThy.add_axioms_cmd);
   173 
   174 fun add_defs ((unchecked, overloaded), args) =
   175   add_axms
   176     (snd oo (if unchecked then PureThy.add_defs_unchecked_cmd else PureThy.add_defs_cmd) overloaded) args;
   177 
   178 
   179 (* declarations *)
   180 
   181 fun declaration pervasive (txt, pos) =
   182   txt |> ML_Context.expression pos
   183     "val declaration: Morphism.declaration"
   184     ("Context.map_proof (Local_Theory.declaration " ^ Bool.toString pervasive ^ " declaration)")
   185   |> Context.proof_map;
   186 
   187 
   188 (* simprocs *)
   189 
   190 fun simproc_setup name lhss (proc, pos) identifier =
   191   ML_Context.expression pos
   192     "val proc: Morphism.morphism -> Simplifier.simpset -> cterm -> thm option"
   193   ("Context.map_proof (Simplifier.def_simproc {name = " ^ ML_Syntax.print_string name ^ ", \
   194     \lhss = " ^ ML_Syntax.print_strings lhss ^ ", proc = proc, \
   195     \identifier = Library.maps ML_Context.thms " ^ ML_Syntax.print_strings identifier ^ "})") proc
   196   |> Context.proof_map;
   197 
   198 
   199 (* hide names *)
   200 
   201 val hide_kinds =
   202  [("class", (Sign.intern_class, can o Sign.certify_class, Sign.hide_class)),
   203   ("type", (Sign.intern_type, Sign.declared_tyname, Sign.hide_type)),
   204   ("const", (Sign.intern_const, Sign.declared_const, Sign.hide_const)),
   205   ("fact", (PureThy.intern_fact, PureThy.defined_fact, PureThy.hide_fact))];
   206 
   207 fun hide_names b (kind, xnames) thy =
   208   (case AList.lookup (op =) hide_kinds kind of
   209     SOME (intern, check, hide) =>
   210       let
   211         val names = map (intern thy) xnames;
   212         val bads = filter_out (check thy) names;
   213       in
   214         if null bads then fold (hide b) names thy
   215         else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
   216       end
   217   | NONE => error ("Bad name space specification: " ^ quote kind));
   218 
   219 
   220 (* goals *)
   221 
   222 fun goal opt_chain goal stmt int =
   223   opt_chain #> goal NONE (K I) stmt int;
   224 
   225 val have = goal I Proof.have;
   226 val hence = goal Proof.chain Proof.have;
   227 val show = goal I Proof.show;
   228 val thus = goal Proof.chain Proof.show;
   229 
   230 
   231 (* local endings *)
   232 
   233 fun local_qed m = Toplevel.proof (Proof.local_qed (m, true));
   234 val local_terminal_proof = Toplevel.proof' o Proof.local_future_terminal_proof;
   235 val local_default_proof = Toplevel.proof Proof.local_default_proof;
   236 val local_immediate_proof = Toplevel.proof Proof.local_immediate_proof;
   237 val local_done_proof = Toplevel.proof Proof.local_done_proof;
   238 val local_skip_proof = Toplevel.proof' Proof.local_skip_proof;
   239 
   240 val skip_local_qed = Toplevel.skip_proof (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF);
   241 
   242 
   243 (* global endings *)
   244 
   245 fun global_qed m = Toplevel.end_proof (K (Proof.global_qed (m, true)));
   246 val global_terminal_proof = Toplevel.end_proof o K o Proof.global_terminal_proof;
   247 val global_default_proof = Toplevel.end_proof (K Proof.global_default_proof);
   248 val global_immediate_proof = Toplevel.end_proof (K Proof.global_immediate_proof);
   249 val global_skip_proof = Toplevel.end_proof Proof.global_skip_proof;
   250 val global_done_proof = Toplevel.end_proof (K Proof.global_done_proof);
   251 
   252 val skip_global_qed = Toplevel.skip_proof_to_theory (fn n => n = 1);
   253 
   254 
   255 (* common endings *)
   256 
   257 fun qed m = local_qed m o global_qed m o skip_local_qed o skip_global_qed;
   258 fun terminal_proof m = local_terminal_proof m o global_terminal_proof m;
   259 val default_proof = local_default_proof o global_default_proof;
   260 val immediate_proof = local_immediate_proof o global_immediate_proof;
   261 val done_proof = local_done_proof o global_done_proof;
   262 val skip_proof = local_skip_proof o global_skip_proof;
   263 
   264 
   265 (* init and exit *)
   266 
   267 fun init_theory (name, imports, uses) =
   268   Toplevel.init_theory name (ThyInfo.begin_theory name imports (map (apfst Path.explode) uses))
   269     (fn thy =>
   270       if ThyInfo.check_known_thy (Context.theory_name thy)
   271       then ThyInfo.end_theory thy else ());
   272 
   273 val exit = Toplevel.keep (fn state =>
   274  (Context.set_thread_data (try Toplevel.generic_theory_of state);
   275   raise Toplevel.TERMINATE));
   276 
   277 val quit = Toplevel.imperative quit;
   278 
   279 
   280 (* print state *)
   281 
   282 fun set_limit _ NONE = ()
   283   | set_limit r (SOME n) = r := n;
   284 
   285 fun pr (modes, (lim1, lim2)) = Toplevel.keep (fn state =>
   286   (set_limit goals_limit lim1; set_limit ProofContext.prems_limit lim2; Toplevel.quiet := false;
   287     PrintMode.with_modes modes (Toplevel.print_state true) state));
   288 
   289 val disable_pr = Toplevel.imperative (fn () => Toplevel.quiet := true);
   290 val enable_pr = Toplevel.imperative (fn () => Toplevel.quiet := false);
   291 
   292 
   293 (* diagnostic ML evaluation *)
   294 
   295 fun ml_diag verbose (txt, pos) = Toplevel.keep (fn state =>
   296   (ML_Context.eval_in
   297     (Option.map Context.proof_of (try Toplevel.generic_theory_of state)) verbose pos txt));
   298 
   299 
   300 (* current working directory *)
   301 
   302 fun cd path = Toplevel.imperative (fn () => File.cd path);
   303 val pwd = Toplevel.imperative (fn () => writeln (Path.implode (File.pwd ())));
   304 
   305 
   306 (* present draft files *)
   307 
   308 fun display_drafts files = Toplevel.imperative (fn () =>
   309   let val outfile = File.shell_path (Present.drafts (getenv "ISABELLE_DOC_FORMAT") files)
   310   in File.isabelle_tool "display" ("-c " ^ outfile ^ " &"); () end);
   311 
   312 fun print_drafts files = Toplevel.imperative (fn () =>
   313   let val outfile = File.shell_path (Present.drafts "ps" files)
   314   in File.isabelle_tool "print" ("-c " ^ outfile); () end);
   315 
   316 
   317 (* pretty_setmargin *)
   318 
   319 fun pretty_setmargin n = Toplevel.imperative (fn () => Pretty.setmargin n);
   320 
   321 
   322 (* print parts of theory and proof context *)
   323 
   324 val print_context = Toplevel.keep Toplevel.print_state_context;
   325 
   326 fun print_theory verbose = Toplevel.unknown_theory o
   327   Toplevel.keep (Pretty.writeln o Proof_Display.pretty_full_theory verbose o Toplevel.theory_of);
   328 
   329 val print_syntax = Toplevel.unknown_context o
   330   Toplevel.keep (ProofContext.print_syntax o Toplevel.context_of);
   331 
   332 val print_abbrevs = Toplevel.unknown_context o
   333   Toplevel.keep (ProofContext.print_abbrevs o Toplevel.context_of);
   334 
   335 val print_facts = Toplevel.unknown_context o Toplevel.keep (fn state =>
   336   ProofContext.setmp_verbose_CRITICAL
   337     ProofContext.print_lthms (Toplevel.context_of state));
   338 
   339 val print_configs = Toplevel.unknown_context o Toplevel.keep (fn state =>
   340   Attrib.print_configs (Toplevel.context_of state));
   341 
   342 val print_theorems_proof = Toplevel.keep (fn state =>
   343   ProofContext.setmp_verbose_CRITICAL
   344     ProofContext.print_lthms (Proof.context_of (Toplevel.proof_of state)));
   345 
   346 fun print_theorems_theory verbose = Toplevel.keep (fn state =>
   347   Toplevel.theory_of state |>
   348   (case Toplevel.previous_context_of state of
   349     SOME prev => Proof_Display.print_theorems_diff verbose (ProofContext.theory_of prev)
   350   | NONE => Proof_Display.print_theorems verbose));
   351 
   352 fun print_theorems verbose =
   353   Toplevel.unknown_context o print_theorems_theory verbose o print_theorems_proof;
   354 
   355 val print_locales = Toplevel.unknown_theory o
   356   Toplevel.keep (Locale.print_locales o Toplevel.theory_of);
   357 
   358 fun print_locale (verbose, name) = Toplevel.unknown_theory o
   359   Toplevel.keep (fn state => Locale.print_locale (Toplevel.theory_of state) verbose name);
   360 
   361 fun print_registrations name = Toplevel.unknown_context o
   362   Toplevel.keep (fn state =>
   363     Locale.print_registrations (Toplevel.theory_of state) name);
   364 
   365 val print_attributes = Toplevel.unknown_theory o
   366   Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
   367 
   368 val print_simpset = Toplevel.unknown_context o
   369   Toplevel.keep (fn state =>
   370     let val ctxt = Toplevel.context_of state
   371     in Pretty.writeln (Simplifier.pretty_ss ctxt (simpset_of ctxt)) end);
   372 
   373 val print_rules = Toplevel.unknown_context o
   374   Toplevel.keep (Context_Rules.print_rules o Toplevel.context_of);
   375 
   376 val print_trans_rules = Toplevel.unknown_context o
   377   Toplevel.keep (Calculation.print_rules o Toplevel.context_of);
   378 
   379 val print_methods = Toplevel.unknown_theory o
   380   Toplevel.keep (Method.print_methods o Toplevel.theory_of);
   381 
   382 val print_antiquotations = Toplevel.imperative ThyOutput.print_antiquotations;
   383 
   384 val thy_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   385   let
   386     val thy = Toplevel.theory_of state;
   387     val all_thys = sort ThyInfo.thy_ord (thy :: Theory.ancestors_of thy);
   388     val gr = all_thys |> map (fn node =>
   389       let
   390         val name = Context.theory_name node;
   391         val parents = map Context.theory_name (Theory.parents_of node);
   392         val dir = Present.session_name node;
   393         val unfold = not (ThyInfo.known_thy name andalso ThyInfo.is_finished name);
   394       in {name = name, ID = name, parents = parents, dir = dir, unfold = unfold, path = ""} end);
   395   in Present.display_graph gr end);
   396 
   397 val class_deps = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   398   let
   399     val thy = Toplevel.theory_of state;
   400     val {classes = (space, algebra), ...} = Type.rep_tsig (Sign.tsig_of thy);
   401     val {classes, ...} = Sorts.rep_algebra algebra;
   402     fun entry (c, (i, (_, cs))) =
   403       (i, {name = Name_Space.extern space c, ID = c, parents = cs,
   404             dir = "", unfold = true, path = ""});
   405     val gr =
   406       Graph.fold (cons o entry) classes []
   407       |> sort (int_ord o pairself #1) |> map #2;
   408   in Present.display_graph gr end);
   409 
   410 fun thm_deps args = Toplevel.unknown_theory o Toplevel.keep (fn state =>
   411   Thm_Deps.thm_deps (Proof.get_thmss (Toplevel.enter_proof_body state) args));
   412 
   413 
   414 (* find unused theorems *)
   415 
   416 fun unused_thms opt_range = Toplevel.keep (fn state =>
   417   let
   418     val thy = Toplevel.theory_of state;
   419     val ctxt = Toplevel.context_of state;
   420     fun pretty_thm (a, th) = ProofContext.pretty_fact ctxt (a, [th]);
   421   in
   422     Thm_Deps.unused_thms
   423       (case opt_range of
   424         NONE => (Theory.parents_of thy, [thy])
   425       | SOME (xs, NONE) => (map ThyInfo.get_theory xs, [thy])
   426       | SOME (xs, SOME ys) => (map ThyInfo.get_theory xs, map ThyInfo.get_theory ys))
   427     |> map pretty_thm |> Pretty.chunks |> Pretty.writeln
   428   end);
   429 
   430 
   431 (* print proof context contents *)
   432 
   433 val print_binds = Toplevel.unknown_context o Toplevel.keep (fn state =>
   434   ProofContext.setmp_verbose_CRITICAL ProofContext.print_binds (Toplevel.context_of state));
   435 
   436 val print_cases = Toplevel.unknown_context o Toplevel.keep (fn state =>
   437   ProofContext.setmp_verbose_CRITICAL ProofContext.print_cases (Toplevel.context_of state));
   438 
   439 
   440 (* print theorems, terms, types etc. *)
   441 
   442 local
   443 
   444 fun string_of_stmts state args =
   445   Proof.get_thmss state args
   446   |> map (Element.pretty_statement (Proof.context_of state) Thm.theoremK)
   447   |> Pretty.chunks2 |> Pretty.string_of;
   448 
   449 fun string_of_thms state args =
   450   Pretty.string_of (Display.pretty_thms (Proof.context_of state) (Proof.get_thmss state args));
   451 
   452 fun string_of_prfs full state arg =
   453   Pretty.string_of
   454     (case arg of
   455       NONE =>
   456         let
   457           val {context = ctxt, goal = thm} = Proof.simple_goal state;
   458           val thy = ProofContext.theory_of ctxt;
   459           val prf = Thm.proof_of thm;
   460           val prop = Thm.full_prop_of thm;
   461           val prf' = Proofterm.rewrite_proof_notypes ([], []) prf;
   462         in
   463           Proof_Syntax.pretty_proof ctxt
   464             (if full then Reconstruct.reconstruct_proof thy prop prf' else prf')
   465         end
   466     | SOME args => Pretty.chunks
   467         (map (Proof_Syntax.pretty_proof_of (Proof.context_of state) full)
   468           (Proof.get_thmss state args)));
   469 
   470 fun string_of_prop state s =
   471   let
   472     val ctxt = Proof.context_of state;
   473     val prop = Syntax.read_prop ctxt s;
   474     val ctxt' = Variable.auto_fixes prop ctxt;
   475   in Pretty.string_of (Pretty.quote (Syntax.pretty_term ctxt' prop)) end;
   476 
   477 fun string_of_term state s =
   478   let
   479     val ctxt = Proof.context_of state;
   480     val t = Syntax.read_term ctxt s;
   481     val T = Term.type_of t;
   482     val ctxt' = Variable.auto_fixes t ctxt;
   483   in
   484     Pretty.string_of
   485       (Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t), Pretty.fbrk,
   486         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' T)])
   487   end;
   488 
   489 fun string_of_type state s =
   490   let
   491     val ctxt = Proof.context_of state;
   492     val T = Syntax.read_typ ctxt s;
   493   in Pretty.string_of (Pretty.quote (Syntax.pretty_typ ctxt T)) end;
   494 
   495 fun print_item string_of (modes, arg) = Toplevel.keep (fn state =>
   496   PrintMode.with_modes modes (fn () =>
   497     writeln (string_of (Toplevel.enter_proof_body state) arg)) ());
   498 
   499 in
   500 
   501 val print_stmts = print_item string_of_stmts;
   502 val print_thms = print_item string_of_thms;
   503 val print_prfs = print_item o string_of_prfs;
   504 val print_prop = print_item string_of_prop;
   505 val print_term = print_item string_of_term;
   506 val print_type = print_item string_of_type;
   507 
   508 end;
   509 
   510 
   511 (* markup commands *)
   512 
   513 fun check_text (txt, pos) state =
   514  (Position.report Markup.doc_source pos;
   515   ignore (ThyOutput.eval_antiquote (#1 (OuterKeyword.get_lexicons ())) state (txt, pos)));
   516 
   517 fun header_markup txt = Toplevel.keep (fn state =>
   518   if Toplevel.is_toplevel state then check_text txt state
   519   else raise Toplevel.UNDEF);
   520 
   521 fun local_theory_markup (loc, txt) = Toplevel.present_local_theory loc (check_text txt);
   522 val proof_markup = Toplevel.present_proof o check_text;
   523 
   524 end;